Literal text → safe regex source

Regex Escape Tool

Turn literal text into regex-safe output for JavaScript. Escape metacharacters, generate a RegExp constructor or regex literal, and optionally anchor the result when the entire input must match exactly.

Private by design: escaping and output generation happen locally in your browser.

Regex source

example\.com/path\?foo=1&value=\(test\)

JavaScript regex literal

/example\.com\/path\?foo=1&value=\(test\)/

JavaScript RegExp constructor

new RegExp("example\\.com/path\\?foo=1&value=\\(test\\)")
✓ Literal round-trip passesThe generated source is tested against the original input.

Which regex characters are escaped?

The regex source escapes characters that otherwise have special meaning in JavaScript regular expressions: . * + ? ^ $ { } ( ) | [ ] \\. When producing a JavaScript /.../ regex literal, the forward slash delimiter is escaped too.

Input:  price is $5.00 (sale?)
Source: price is \$5\.00 \(sale\?\)

Use escaped input when matching literally

If a user searches for example.com, an unescaped dot means “any character” in regex syntax. Escaping changes it to example\\.com, so the pattern means the exact punctuation the user entered.

After generating the source, open the Regex Tester when you want to combine the literal fragment with your own regex structure.

Escaping does not validate semantics

This tool intentionally converts text into a literal-safe regex fragment. It does not decide whether the input is a valid URL, email address, UUID, date, or other structured value. For those jobs, use the Regex Pattern Library or a purpose-built parser.

Typical uses for regex escaping

Search boxes

Turn literal search text into a safe regex fragment before adding optional matching behavior such as case-insensitivity.

Code generation

Generate valid regex source for configuration, snippets, tests, or JavaScript RegExp constructors.

Dynamic filters

Escape data-driven labels, file names, routes, or identifiers before combining them with controlled regex syntax.

Frequently asked questions

What characters need to be escaped in a regex?

In ordinary JavaScript regex source, metacharacters such as . * + ? ^ $ { } ( ) | [ ] and \ need escaping when you want them to mean literal characters. A forward slash also needs escaping when the expression is written as a /.../ JavaScript regex literal.

Why is escaping different for RegExp() and /regex/ literals?

The RegExp constructor receives a JavaScript string, so the regex backslashes must survive JavaScript string escaping. A regex literal does not use a quoted string, but the / delimiter itself must be escaped inside the literal.

Does this tool escape an entire string literally?

Yes. The generated regex source is designed to match the entered text literally rather than interpreting regex metacharacters inside it. You can optionally add ^ and $ anchors to require a whole-string match.

Should user input be inserted directly into a regex?

Not without escaping if the intent is literal matching. Unescaped user input can change regex meaning and may create unexpected matching behavior. For complex or security-sensitive patterns, also review performance and input-length limits.