JavaScript Regex Escape and RegExp Converter

Escape plain text for a literal search, or wrap a regex you already wrote in a JavaScript string. The converter runs in your browser.

Input type

How to test a new RegExp string source

  1. Decide whether production code will use a regex literal or `new RegExp(source, flags)`.
  2. Test the raw pattern in the online tester without slash delimiters, using the same JavaScript flags your code will pass.
  3. Convert the raw pattern into a JavaScript string source by doubling backslashes that must survive into regex syntax.
  4. Run the same pass and fail samples against the constructor version before copying it into app code.
  5. Keep user-provided text out of `new RegExp()` until it has been escaped as literal text.

Compare the raw pattern, literal, and constructor

Tester pattern

\b(error|warning)\b\s*:\s*(.+)

Paste this raw pattern into the live tester field. Do not include slash delimiters.

Regex literal

/\b(error|warning)\b\s*:\s*(.+)/gim

Use this form when the expression is static in JavaScript or TypeScript source.

RegExp constructor

new RegExp("\\b(error|warning)\\b\\s*:\\s*(.+)", "gim")

Use this form when the pattern is assembled from strings, config, or escaped user input.

JavaScript regex escaping examples

Word boundary

Raw pattern

\bword\b

Constructor source

"\\bword\\b"

A single `\b` inside a JavaScript string is a backspace escape, not a regex word boundary.

Digits and dates

Raw pattern

^(\d{4})-(\d{2})-(\d{2})$

Constructor source

"^(\\d{4})-(\\d{2})-(\\d{2})$"

`\d` must stay a regex digit token after the string literal is evaluated.

Literal dot

Raw pattern

^v\d+\.\d+$

Constructor source

"^v\\d+\\.\\d+$"

`\.` must survive as a literal dot check instead of becoming a wildcard.

Path prefix

Raw pattern

^/api/v\d+/users$

Constructor source

"^/api/v\\d+/users$"

Forward slashes do not need escaping in constructor strings, but backslash tokens still do.

Named groups

Raw pattern

^(?<level>error|warn):\s*(?<message>.+)$

Constructor source

"^(?<level>error|warn):\\s*(?<message>.+)$"

Group syntax can stay readable while whitespace tokens are still escaped correctly.

Unicode property escape

Raw pattern

^\p{Letter}+$

Constructor source

"^\\p{Letter}+$" with flag "u"

Property escapes need both a surviving backslash and the JavaScript `u` flag.

Constructor escape checklist

  • A literal regex like `/\d+/` becomes a constructor string like `"\\d+"`.
  • Slash delimiters are syntax for regex literals; do not paste `/pattern/flags` into the tester pattern field.
  • The flags string stays separate in constructor syntax: `new RegExp(source, "gim")`.
  • Use `String.raw` only when the team understands template-literal behavior and interpolation risk.
  • Escape dynamic text before building a regex so characters like `.`, `*`, `?`, `(`, and `[` are treated literally.
  • Retest multiline and Unicode samples with the same flags you will use in code.

Copy-ready constructor examples

RegExp.escape in the ECMAScript specification

Literal and constructor should match the same samples

const literal = /\b(error|warning)\b\s*:\s*(.+)/gim;
const constructor = new RegExp("\\b(error|warning)\\b\\s*:\\s*(.+)", "gim");

const input = "Error: disk full\nwarning: retry queued";
console.log([...input.matchAll(literal)].length);
console.log([...input.matchAll(constructor)].length);

If these counts differ, the string source or flags changed during conversion.

Escape a search term before building a regex

const term = "v2.0 (beta)";
const source = RegExp.escape(term);
const pattern = new RegExp(source, "u");

RegExp.escape treats the input as literal text, including the period and parentheses. It also escapes a leading ASCII letter or digit so the result can follow a regex escape such as a backreference.

String.raw can reduce visual noise for static sources

const source = String.raw`\b(error|warning)\b\s*:\s*(.+)`;
const pattern = new RegExp(source, "gim");

Keep interpolated values out of raw templates unless they have been escaped first.

Check a constructor pattern now

Open the live tester with a log-level expression already loaded. Compare the raw pattern against the constructor source in the sample text, then copy the constructor form only after the matches line up.

Test RegExp Escaping

Related Pages on Regex Tester

JavaScript RegExp Constructor Escape FAQ

Why does new RegExp need double backslashes?

JavaScript string literals consume backslashes before the RegExp engine sees the pattern. Doubling a backslash, such as `\\d`, leaves `\d` available for regex digit matching.

Should I use a regex literal or the RegExp constructor?

Use a regex literal for static patterns. Use the RegExp constructor when the source must be assembled from config, feature flags, or escaped user text.

Can I pass user input directly into new RegExp?

For a literal search term, pass the text through RegExp.escape before passing it to new RegExp. Pass a pattern directly when you want its regex syntax to remain active.