Regex escape

Useful when you build a pattern from user input, a URL or a price that contains dots and brackets.

Loading the tool…

Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.

How to use it

  1. Enter text.
  2. The result appears straight away and updates as you edit.
  3. Copy the result, or copy a link that keeps the same values to share it.

How it is calculated

The characters \ ^ $ . * + ? ( ) [ ] { } | / get a backslash in front, a newline becomes \n and a carriage return \r. All other characters stay as they are.

Example

Result: Escaped pattern: price: \$9\.99 \(VAT 15%\).

price: $9.99 (VAT 15%)
price: \$9\.99 \(VAT 15%\)

Limits and caveats

  • The output targets JavaScript; other engines may need extra escapes, such as # in extended mode.
  • The hyphen is not escaped, since \- outside a character class is an error with the u flag.

Questions about this tool

Why is the dot escaped?

In regex a dot matches any character, so 9.99 also matches 9x99. Escaped as \. it matches only a dot.

Can I pass the result to new RegExp?

Yes, pass the escaped pattern as a string. If you type it inside a JavaScript string literal, double every backslash.

Why is the slash / escaped?

So the pattern works inside a /pattern/ literal without ending early. Escaping it is allowed in every mode.