I want to do a global string replacement using String.replace in Javascript.
In the documentation, I read that I can do this with / g, i.e., for example,
var mystring = mystring.replace(/test/g, mystring);
and this will replace all occurrences inside the mystring. No quotation marks for expression.
But if I have a variable that can be found, how can I do this without quotes?
I tried something like this:
var stringToFind = "test";
// try first
mystring = mystring.replace('/' + stringToFind + '/g', mystring);
// second attempt, not much sense
mystring = mystring.replace(/stringToFind/g, mystring);
but they do not work. Any ideas?
javascript string regex
avastreg Feb 12 '09 at 16:49 2009-02-12 16:49
source share