Thread: regex variable
View Single Post
Old 12-07-2012, 03:21 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by WolfShade View Post
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.
How is that different from / better than

Code:
var txt = "Here is something and something and something";
txt = txt.replace(/(\s)?something/gi,"");
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote