One thing I did notice, however, is that if you're trying to replace more than just one instance of something, you can split on something, then join on "".

s.split(something);
s.join("");

This will replace all instances of variable something with "". If you have two or more spaces in the resulting string, then use regex to replace all double-or-more instances of space with just single space.

12-07-2012, 02:14 PM

007julien

Use the constructor RegExp with something like this

Code:

var something = "foobar";
var rgx = new RegExp(something,"gi");

You need to escape the 11 following characters (often called "metacharacters") with a backslash :

the opening square bracket [,

the backslash \,

the caret ^,

the dollar sign $,

the period or dot .,

the vertical bar or pipe symbol |,

the question mark ?,

the asterisk or star *,

the plus sign +,

the opening round bracket (

and the closing round bracket ).

Then the script is a little more complicated (if the string contains metacharacters)

<input type = "text" id = "txt1" value = "Now is the time for all good men to come to the aid of their country." size = "70">
<br><br>
<textarea id="txt2" cols="40" rows="5">
Now is the time for all good men
to come to the aid of their country.
</textarea>
<br><br>
<div id="divText">
Now is the time for all good men<br>
to come to the aid of their country.
</div>
<br>
Find: <input type="text" id="txt2find" value=""><br>
Replace: <input type="text" id="reptext" value=""><br>
<br><br>
<button
onclick="doRep('txt1',document.getElementById('txt2find').value,document.getElementById('reptext').value, 'T')">
Textbox: Do it</button>
<button
onclick="doRep('txt2',document.getElementById('txt2find').value,document.getElementById('reptext').value, 'T')">
Textarea: Do it</button>
<button
onclick="doRep('divText',document.getElementById('txt2find').value,document.getElementById('reptext').value, 'D')">
DIV: Do it</button>

</body>
</html>

The 11 meta-characters mentioned by julien007 must be escaped in the "to find" string but not of course in the "replace by" string.

12-07-2012, 03:21 PM

Philip M

Quote:

Originally Posted by WolfShade

I don't think you can use a variable in regex.

One thing I did notice, however, is that if you're trying to replace more than just one instance of something, you can split on something, then join on "".

s.split(something);
s.join("");

This will replace all instances of variable something with "". If you have two or more spaces in the resulting string, then use regex to replace all double-or-more instances of space with just single space.

+ // (1)
"\n\nThis is a simple example with a simple string" +
"\nThis is a simple example with a simple string".replace(eval("/"+var1+"/gi"),var2)+" "

+ // -OR- (2)
"\n\nThis is a simple example with a very simple really simple simple simple and I mean simple string"+
"\nThis is a simple example with a very simple really simple simple simple and I mean simple string".replace(eval("/"+var1+"/gi"),
function(n){
return i++%2?var3[cnt++]:n; //replace every other value
})