Thread: regex variable
View Single Post
Old 12-09-2012, 01:38 PM   PM User | #19
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
I forgott the / which is also to escape with javascript !

Code:
String.prototype.toRegExp=function(){return this.replace(/\\/g,'\\\\').replace(/(\[|\^|\$|\.|\||\?|\*|\+|\(|\)|\/)/g,'\\$1')}

//Example of use with all the «meta»characters
var something="[\\^$.|?*+()/"; // the slashe is to escape
	
alert(("foo"+something+"bar").replace(new RegExp(something.toRegExp(),'g'),''));// =>'foobar'
007julien is offline   Reply With Quote