User text inside new RegExp? RegExp.escape keeps a dot a dot
If you put text inside new RegExp without escaping it, every dot becomes "any character" and other symbols change the pattern. RegExp.escape escapes them for you, and it is Baseline 2025 in browsers.

You have a function that removes links to one site from a text, and it builds the regex from the domain name: new RegExp(domain). You try it and it works. But the domain has dots, and a dot in a regex means "any character", so the function now removes other domains that have nothing to do with it. In this article you see how RegExp.escape() fixes that in one line.
What is RegExp.escape()?
RegExp.escape() is a static method on RegExp that escapes every character with a regex meaning in a string, and returns a new string that can be used safely as a literal pattern inside new RegExp().
According to the MDN page on RegExp.escape, the characters escaped with a \ in front are ^ $ \ . * + ? ( ) [ ] { } | and the /. Other characters such as -, = and # are escaped with the \x form.
What breaks if you do not escape the text?
The same page has an example of this mistake: a function that removes developer.mozilla.org links from a text, and builds the pattern straight from the domain name. The dot in the name became "any character", so the function also removed a developer-mozilla.org link, which is a different site.
// before: the dot matches any character
const pattern = new RegExp(`https?://${domain}(?=/)`, 'g');
// after: the dot stays a dot
const safe = new RegExp(`https?://${RegExp.escape(domain)}(?=/)`, 'g');And if the text comes from the user, like a search field, the problem is bigger: the user can type characters that change the whole pattern, not only a dot.
What does RegExp.escape() return?
The result is a string that looks a bit strange, but it matches the original text exactly. From the page's examples:
Input | Result |
|---|---|
|
|
|
|
|
|
The \x66 is the f. The first character of the string, if it is a digit or an ASCII letter, is escaped with the \x form, so it is not read as part of an escape that comes before it in the pattern.
And if you pass it something that is not a string, it throws a TypeError.
Where does it work?
RegExp.escape() is Baseline 2025 according to the same page: it works in the latest browser versions since May 2025, and it may not work in older devices or browsers.
Where do you start now?
The lesson for you as a developer: any text you put inside a regex is code, not text, unless you escape it.
Search your code for
new RegExp(with a variable inside.If the variable has to match literally, wrap it in
RegExp.escape().If you have to support older browsers, check that the method exists before you use it.
The same idea, that input must not change the meaning of the code, is the core of the SQL injection article. If you check URLs from users, see the open redirect article. And if you are building your JavaScript basics, there is the 2026 developer roadmap.
Where in your code is there a new RegExp() built from a variable?
FAQ
Why does RegExp.escape change the first letter of the text?
If the first character is a digit or an ASCII letter, it is escaped with the \x form, so that if the text lands right after another escape in the pattern, it is not read as part of it. The result still matches the same text.
What happens if I pass it a number instead of a string?
It throws a TypeError. Turn the value into a string first, for example String(value).
Does it work in every browser?
In the latest browser versions, yes. The MDN page says it is Baseline 2025 since May 2025, and it may not work in older devices or browsers.


