Last active
October 11, 2015 05:37
-
-
Save sstur/3810616 to your computer and use it in GitHub Desktop.
RegExp Escape
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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