Skip to content

Instantly share code, notes, and snippets.

@hotoo
Created September 23, 2011 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotoo/1237211 to your computer and use it in GitHub Desktop.
Save hotoo/1237211 to your computer and use it in GitHub Desktop.
String.prototype.escRegExp = (function(){
var _ESC = null,
_ESC_DATA = "! \\ / . $ * ^ ( ) [ ] { } ? + - |".split(" ");
function init(){
_ESC = [];
for(var i=0, l=_ESC_DATA.length; i<l; i++){
_ESC[i] = new RegExp("\\"+_ESC_DATA[i], "g");
}
return _ESC;
}
return function(){
var a=_ESC || init(), s=this;
for (var i=0, l=a.length; i<l; i++){
s = s.replace(a[i], "\\"+_ESC_DATA[i]);
}
return s;
}
})();
String._ESC_DATA = "! \\ / . $ * ^ ( ) [ ] { } ? + - |";
String.prototype.toEsc = function(){
var a=String._ESC_DATA.split(" "), s=this;
for (var i=0, l=a.length; i<l; i++){
s = s.replace(new RegExp("\\"+a[i], "g"), "\\"+a[i]);
}
return s;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment