34

I know that str.replace(/x/g, "y")replaces all x's in the string but I want to do this

function name(str,replaceWhat,replaceTo){
    str.replace(/replaceWhat/g,replaceTo);
}

How can i use a variable in the first argument?

Cœur
  • 37,241
  • 25
  • 195
  • 267
usama8800
  • 893
  • 3
  • 10
  • 20

2 Answers2

64

The RegExp constructor takes a string and creates a regular expression out of it.

function name(str,replaceWhat,replaceTo){
    var re = new RegExp(replaceWhat, 'g');
    return str.replace(re,replaceTo);
}

If replaceWhat might contain characters that are special in regular expressions, you can do:

function name(str,replaceWhat,replaceTo){
    replaceWhat = replaceWhat.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    var re = new RegExp(replaceWhat, 'g');
    return str.replace(re,replaceTo);
}

See Is there a RegExp.escape function in Javascript?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    This doesn't work as expected if replaceWhat contains regexp metacharacters, e.g. `*`, `+`, `[`. – pts Jul 23 '13 at 20:12
  • @pts So does a literal regexp. – Barmar Jul 23 '13 at 20:16
  • Then add this logic in as well: [Is there a RegExp.escape function in Javascript?](http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript) – dc5 Jul 23 '13 at 20:24
  • If used like the asker intended, this will give wrong results if metacharacters appear in replaceWhat. So better chose another answer! – dronus Feb 12 '14 at 20:28
  • @dronus Isn't that what pts said in his comment? dc5 pointed to a workaround for that. – Barmar Feb 12 '14 at 20:33
  • Yes, it is exactly what pts said. But that doesn't make the answer itself valid... maybe you like to edit it? – dronus Feb 12 '14 at 20:46
  • https://www.npmjs.com/package/stringinject – codebytom Jul 19 '17 at 16:06
  • remember, you need to bind this to a new variable : https://stackoverflow.com/questions/12231644/js-replace-not-working-on-string?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – thatOneGuy Apr 11 '18 at 15:05
  • 1
    @thatOneGuy Good catch, I've added a `return` to the functions. I was obviously focused on the regexp part, not the replace part. – Barmar Apr 11 '18 at 16:09
0

The third parameter of flags below was removed from browsers a few years ago and this answer is no longer needed -- now replace works global without flags


Replace has an alternate form that takes 3 parameters and accepts a string:

function name(str,replaceWhat,replaceTo){
    str.replace(replaceWhat,replaceTo,"g");
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • 5
    This is Firefox-specific, in Chrome it replaces only the first occurrence. – pts Jul 23 '13 at 20:14
  • True... it had the "nonstandard" flag. – Hogan Jul 23 '13 at 21:41
  • remember, you need to bind this to a new variable : https://stackoverflow.com/questions/12231644/js-replace-not-working-on-string?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – thatOneGuy Apr 11 '18 at 15:05
  • @Hogan this is not working currently please remove this answer or update with working solution. – Freddy Daniel Nov 04 '19 at 06:21