Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท RegExp

RegExp.escape()

Reference for RegExp.escape(): turn any string into text that a regular expression matches literally, with examples, a fallback for old browsers and notes.

RegExp.escape(string) returns a copy of the string in which characters that have a special meaning in a regular expression are escaped. Building a pattern from user input without escaping is both a bug (a search for a.b also matches axb) and a risk (an input such as ( throws a SyntaxError, and a crafted input can make the pattern very slow).

The escaped result is meant to be placed inside a larger pattern, for example with new RegExp(RegExp.escape(term), "gi"). It is a fairly new addition to the language, so the page includes a fallback.

JavaScript
const safe = RegExp.escape("1+1=2 (really?)");
const re = new RegExp(safe);

Try it

JS Literal search for user input

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Highlighting a search term

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
RegExp.escape(string)
Name Description
string The text to escape. It must be a string.

Return value: a new string that, used inside a regular expression, matches the original text exactly.

Exceptions: a TypeError if the argument is not a string. Numbers, null and objects are not converted; call String(value) yourself.

Usage notes

  • What gets escaped. The pattern syntax characters (^ $ \ . * + ? ( ) [ ] { } |) and / get a backslash. Some other punctuation, whitespace, and a leading letter or digit are written as \x.. or \u.... escapes. The details are deliberate, so the result is safe in more places (for example right after another escape or next to a following digit), but they can change between uses, so do not parse or compare the escaped text; just use it.
  • It escapes for the pattern, not for a character class or replacement string. Inside [...] the safest approach is still to build the set from known characters. For replacement strings use a function replacement, or double the $ signs.
  • replaceAll() with a string needs no escaping. text.replaceAll("a.b", "x") already treats the first argument as plain text. Escaping is needed only when you must build a RegExp, for example for the i flag or word boundaries.
  • Fallback. The common one-liner s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") is good enough for ordinary use. Its output differs from RegExp.escape(), and escaping characters that are not syntax characters (such as - or /) is only safe without the u flag, because u patterns reject unknown escapes like \- outside a character class. The fallback in the examples above is meant for patterns built without u.
  • It does not limit length or complexity. Escaping makes text literal, so it cannot create regex backtracking problems by itself, but a huge needle is still a huge pattern.

Browser support

RegExp.escape() is a recent addition: support is limited to newer browser and Node versions, so check a compatibility table and keep the fallback shown above for older environments.