Created
August 28, 2018 14:30
-
-
Save aMarCruz/58670d97ca53155eab41ce22c021a4f9 to your computer and use it in GitHub Desktop.
Escape an string for using with a Regex constructor
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
function sanitizeForRegex (str) { | |
return String(str).replace(/(?=[.?*+^$[\]\\(){}|-])/g, '\\') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment