Given the string 'Hello ?, welcome to ?'
and the array ['foo', 'bar']
, how do I get the string 'Hello foo, welcome to bar'
in a single line of code with JavaScript (possibly with jQuery, Underscore, etc.)?
longstring = longstring. replace(replacestring, ""); And it will all work.
String.prototype.replaceAll() The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
var s = 'Hello ?, welcome to ?';
var a = ['foo', 'bar'];
var i = 0;
alert(s.replace(/\?/g,function(){return a[i++]}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With