Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Created January 12, 2012 09:33
Show Gist options
  • Save JamieMason/1599574 to your computer and use it in GitHub Desktop.
Save JamieMason/1599574 to your computer and use it in GitHub Desktop.
Get a RegExp from an unescaped RegExp String
function escapedRx (text, flags)
{
return new RegExp(text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), flags || '');
}
@JamieMason
Copy link
Author

var matchGoogleUrl = escapedRx('http://www.google.com');
'http://www.google.com'.match(matchGoogleUrl)
// >> ["http://www.google.com"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment