RegExp escape function
RegExp.escape = (function() {
var punctuationChars = /([.*+?|/(){}[\]\\])/g;
return function(text) {
return text.replace(punctuationChars, '\\$1');
}
})();
var str = RegExp.escape('a+b/c*d$ ^{.}');
var reg = new RegExp(str);
var punctuationChars = /([.*+?|/(){}[\]\\])/g;
return function(text) {
return text.replace(punctuationChars, '\\$1');
}
})();
var str = RegExp.escape('a+b/c*d$ ^{.}');
var reg = new RegExp(str);
Reference: http://simonwillison.net/2006/Jan/20/escape
No comments :
Post a Comment