Query guide: JavaScript RegExp constructor escaping
JavaScript RegExp constructor escape tester for string-based expressions
Use this page when a JavaScript regex works as a literal but breaks after moving into new RegExp(source, flags). Test the raw expression online, compare the constructor string source, and catch missing backslashes before the regex ships.
Searches for check regex javascript, regex tester javascript, and regex tester expressions often hide one practical problem: the pattern is correct, but the JavaScript string that carries it is not. The regex engine only sees the string after JavaScript evaluates escapes, so tokens such as \\b, \\d, \\s, and \\p need special attention.
The safest workflow is to test the raw regex first, then test the exact constructor source and flags your browser, TypeScript, or Node.js code will use. That separates regex logic bugs from JavaScript escaping bugs.
This guide focuses on constructor escaping. Use the live tester to prove the pattern behavior, then keep the same sample strings beside the final RegExp code.
How to test a new RegExp string source
- Decide whether production code will use a regex literal or `new RegExp(source, flags)`.
- Test the raw pattern in the online tester without slash delimiters, using the same JavaScript flags your code will pass.
- Convert the raw pattern into a JavaScript string source by doubling backslashes that must survive into regex syntax.
- Run the same pass and fail samples against the constructor version before copying it into app code.
- 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, Unicode, and replacement samples because escaping bugs often look like flag bugs.
Copy-ready constructor examples
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 dynamic user text before building a regex
function escapeRegExp(value: string) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
const pattern = new RegExp("\\b" + escapeRegExp(term) + "\\b", "i");Use this when a search term should be literal text, not live regex syntax.
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 EscapingRelated Pages on Regex Tester
Open the live JavaScript regex escape tester
Paste the raw pattern, choose flags, and verify samples before converting to constructor syntax.
JavaScript regex tester
Check broader JavaScript RegExp behavior with match highlights, groups, flags, and replacements.
JavaScript regex checker
Validate the expression behavior your browser, TypeScript, or Node.js code will run.
JavaScript regex flags tester
Confirm that `g`, `i`, `m`, `s`, `u`, and `y` match the constructor flags string.
JavaScript regex.test validator
Check boolean validator behavior after the constructor source is escaped correctly.
JavaScript regex examples
Review literal syntax, constructor syntax, named groups, and Unicode property escapes.
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?
Only when you intentionally want users to write regex syntax. For literal search terms, escape regex metacharacters before constructing the expression.