views:

2098

answers:

8

Hi to all.

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?

+12  A: 
var mystring = "hello world test world";
var find = "world";
var regex = new RegExp(find, "g");
alert(mystring.replace(regex, "yay")); // alerts "hello yay test yay"
Paolo Bergantino
This one helped me!
Ropstah
It also helped me!
KClough
Same here, thanks!
Nicky Hajal
+1  A: 

Try:

var stringToFind = "test";
mystring = mystring.replace(new RegExp(stringToFind, "g"), mystring);
Crescent Fresh
I suppose the last' mystring' in the replace is not what you meant?
KooiInc
+1  A: 

Thats a regular expression, not a string. Use the constructor for a RegExp object to dynamically create a regex.

var r = new RegExp(stringToFind, 'g');
mystring.replace(r, 'some replacement text');
Squeegy
+1  A: 

If you want variables interpolated, you need to use the RegExp object

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions

Example:

var str = "This is my name";
var replace = "i";
var re = new RegExp(replace, 'g')    

str = str.replace(re, 'p');
alert(str);
Phil
+6  A: 

For regex, new RegExp(stringtofind, 'g');. BUT. If ‘find’ contains characters that are special in regex, they will have their regexy meaning. So if you tried to replace the '.' in 'abc.def' with 'x', you'd get 'xxxxxxx' — whoops.

If all you want is a simple string replacement, there is no need for regular expressions! Here is the plain string replace idiom:

mystring= mystring.split(stringtofind).join(replacementstring);
bobince
+1 always good to see people thinking beyond the literal answer to the question.
Thomas
+2  A: 

Can you use prototype.js? If so you could use String.gsub, like

var myStr = "a day in a life of a thing";
 var replace = "a";
 var resultString = myStr.gsub(replace, "g");
 // resultString will be "g day in g life of g thing"

It will also take regular expressions. To me this is one of the more elegant ways to solve it. prototypejs gsub documentation

Heat Miser
+2  A: 
String.prototype.replaceAll = function (replaceThis, withThis) {
   var re = new RegExp(RegExp.quote(replaceThis),"g"); 
   return this.replace(re, withThis);
};


RegExp.quote = function(str) {
     return str.replace(/([.?*+^$[\]\\(){}-])/g, "\\$1");
};

var aa = "qwerr.erer".replaceAll(".","A");
alert(aa);

silmiar post

unigogo
A: 

Thanks to all, i've got the point! Before i didn't know how to manage regex in javascript..

Yes i use the prototype framework, and before posting this question i've searched into the prototype String documentation, but found nothing, i'm so dumb :|