Skip to content

Instantly share code, notes, and snippets.

@robozevel
Forked from nathansmith/escape_regex.js
Created November 25, 2012 00:15
Show Gist options
  • Save robozevel/4141906 to your computer and use it in GitHub Desktop.
Save robozevel/4141906 to your computer and use it in GitHub Desktop.
Escape characters that could break regex.
(function(replace) {
var escape = [/[\-\[\]\/\\{}()*+?.^$|]/g, '\\$&'];
RegExp.Safe = function(pattern, flags) {
return new RegExp(replace.apply(String(pattern), escape), flags);
}
}(String.prototype.replace));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment