Last active
December 10, 2015 05:22
-
-
Save juji/b45a43bef7187c4ddd52 to your computer and use it in GitHub Desktop.
Replace regex special character
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 escapeRegExp(str) { | |
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | |
} | |
function toRegex(str,flag){ | |
return new RegExp(escapeRegExp(str),flag); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment