Skip to content

Instantly share code, notes, and snippets.

@sstur
Last active October 11, 2015 05:37
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 sstur/3810616 to your computer and use it in GitHub Desktop.
Save sstur/3810616 to your computer and use it in GitHub Desktop.
RegExp Escape
RegExp.escape = function(str) {
return str.replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&");
}
//Example: string.replace(new RegExp(RegExp.escape('a&b'), 'g'), 'c&d')
//Credit: http://simonwillison.net/2006/Jan/20/escape/#p-6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment