The primary use case of RegExp.escape()
is when you want to embed a string into a bigger regex pattern, and you want to ensure that the string is treated as a literal pattern, not as a regex syntax. Consider the following naïve example that replaces URLs:
function removeDomain(text, domain) {
return text.replace(new RegExp(`https?://${domain}(?=/)`, "g"), "");
}
const input =
"Consider using [RegExp.escape()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/escape) to escape special characters in a string.";
const domain = "developer.mozilla.org";
console.log(removeDomain(input, domain));
// Consider using [RegExp.escape()](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/escape) to escape special characters in a string.
Inserting the domain
above results in the regular expression literal https?://developer.mozilla.org(?=/)
, where the "." character is a regex wildcard character. This means the string will match the string with any character in place of the ".", such as developer-mozilla-org
. Therefore, it would incorrectly also change the following text:
const input =
"This is not an MDN link: https://developer-mozilla.org/, be careful!";
const domain = "developer.mozilla.org";
console.log(removeDomain(input, domain));
// This is not an MDN link: /, be careful!
To fix this, we can use RegExp.escape()
to ensure that any user input is treated as a literal pattern:
function removeDomain(text, domain) {
return text.replace(
new RegExp(`https?://${RegExp.escape(domain)}(?=/)`, "g"),
"",
);
}
Now this function will do exactly what we intend to, and will not transform developer-mozilla.org
URLs.