Return to Snippet

Revision: 722
at August 2, 2006 10:20 by cochambre


Initial Code
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');
}

Initial URL

                                

Initial Description
Usefull Regular Expressions enhancement. Simplifies standard string operations escaping special chars. And also saves precompiled REs for a speed increase.

Initial Title
Regular Expressions simple strings search/replace escape method (with RE speed enhancment - precompilation)

Initial Tags
replace, text

Initial Language
JavaScript