Skip to content

Instantly share code, notes, and snippets.

@ianpgall
Last active December 20, 2015 12:38
Show Gist options
  • Save ianpgall/6132022 to your computer and use it in GitHub Desktop.
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
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