/ Published in: JavaScript
Usefull Regular Expressions enhancement. Simplifies standard string operations escaping special chars. And also saves precompiled REs for a speed increase.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
RegExp.escape = function(text) { if (!arguments.callee.sRE) { var specials = [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\\\' ]; arguments.callee.sRE = new RegExp( '(\\\\' + specials.join('|\\\\') + ')', 'g' ); } return text.replace(arguments.callee.sRE, '\\\\$1'); }