Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  145] [ 1]  / answers: 1 / hits: 107956  / 16 Years ago, thu, february 12, 2009, 12:00:00

I want to perform a global replace of string 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 mystring. No quotes for the expression.


But if I have a variable to find, how can I do this without quotes?


I've tried something like this:


var stringToFind = "test";

//first try


mystring = mystring.replace('/' + stringToFind + '/g', mystring);

//second try, not much sense at all


mystring = mystring.replace(/stringToFind/g, mystring);

but they don't work. Any ideas?


More From » regex

 Answers
There are no answers for this question yet.
Only authorized users can answer the question. Please sign in first, or register a free account.
austenjordang

Total Points: 544
Total Questions: 112
Total Answers: 112

Location: Monaco
Member since Sun, Jan 16, 2022
2 Years ago
;