Last active
December 20, 2015 12:38
-
-
Save ianpgall/6132022 to your computer and use it in GitHub Desktop.
JavaScript RegExp method to escape a string that will be used in a RegExp object constructor, so the characters aren't interpreted as regex special characters
This file contains hidden or 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.quote = (function () { | |
"use strict"; | |
var ret, re; | |
re = /([.?*+^$[\]\\(){}|-])/g; | |
ret = function (str) { | |
return ("" + str).replace(re, "\\$1"); | |
}; | |
return ret; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment