Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save Alex1990/14b8ca51a88362695ca7 to your computer and use it in GitHub Desktop.

Select an option

Save Alex1990/14b8ca51a88362695ca7 to your computer and use it in GitHub Desktop.
Escape regexp special characters.
/**
* Escape regexp special characters. The slash `/` will be escaped automatically.
* http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript/494122#494122
*/
function escapeRegexp(str) {
return str.replace(/[.*+?^$|[\](){}\\-]/g, '\\$&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment