Thread: regex variable
View Single Post
Old 12-08-2012, 07:42 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
Please do note that if you use the RegExp contructor you must escape the \ characters that are used in the regular expression escapes.

So:
Code:
s.replace(/a\sb/gi,""); // replace all "a b" or "A B" or even "a\tB" with nothing

var re = new RegExp( "a\\sb", "gi" );
s.replace( re, "" );
See the \\s in the constructor version?
That is a little misleading. Should be

Code:
s = s.replace(/a\sb/gi,""); // replace all "a b" or "A B" or even "a\tB" with nothing
var re = new RegExp( "a\\sb", "gi" );
s = s.replace( re, "" );
__________________

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