Skip to content

RegExp.escape — matching user text literally

Level: 201 · for anyone building a regex from text a user typed

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: RegExp.escape (ES2025, in Node 24) turns text into a pattern that matches only itself — new RegExp("1.5") matches "125", the escaped one does not — and it escapes more than punctuation: RegExp.escape("1.5") is \x31\.5.

Keywords: RegExp.escape

What the finished page will answer

  • What does RegExp.escape return for "1.5", "a+b", "c-d, e" and a space, and why is a leading digit or letter written as \x31?
  • Why does new RegExp("1.5") match "125", and what does the escaped pattern match instead?
  • Is the escaped text safe right after a backreference like \1, inside a character class, and in a v-mode pattern?
  • Which input breaks the pre-2025 idiom s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")?
  • Which Node versions have RegExp.escape, and how does a program check before using it?

Examples it will need

  • regexp_escape_outputs_js.js — RegExp.escape of several strings, and a pattern built with and without it tested against 125
  • regexp_escape_old_idiom_js.js — the pre-2025 replace idiom beside RegExp.escape, on a digit placed right after a backreference

See also

Sources to start from