Escape a string for use in a JavaScript regular expression

Type the exact text you want to find and the tool adds a backslash before every regex special character, then gives the result as a plain pattern, a /…/ literal and a new RegExp() call.

Live demo · real result from this tool

Escaped pattern

price: \$9\.99 \(incl\. tax\) \[EU\]

As a regex literal

/price: \$9\.99 \(incl\. tax\) \[EU\]/

As a RegExp constructor call

new RegExp("price: \\$9\\.99 \\(incl\\. tax\\) \\[EU\\]")

Processed on your device — nothing is uploaded.

How to use the JS regex escaper

  1. Type or paste the exact text into Literal text to match.
  2. Keep Also escape / (for /…/ literals) ticked if the pattern will sit between slashes.
  3. Copy the version you need: Escaped pattern, As a regex literal or As a RegExp constructor call.
  4. Add your own flags and anchors, such as ^, $ or /g, after pasting.

What it does and when to use it

Search features, log filters and find-and-replace scripts often build a regex from user input. If the input is $9.99 (incl. tax), the dot matches any character, the brackets create a group and the dollar sign means end of line, so the search fails or matches the wrong thing. Escaping makes every character mean itself.

Test the escaped pattern with the advanced regex tester, and look up any symbol in the regex cheat sheet. If you need to put the pattern in a JavaScript string by hand, the JS Unicode escaper helps with non-ASCII characters.

How it works

The tool puts a backslash before each of these characters:

\ ^ $ . * + ? ( ) [ ] { } |

and also / when the box is ticked. It then builds three outputs. The regex literal wraps the pattern in slashes and makes sure any / is escaped. The constructor call passes the pattern through JSON.stringify, which doubles every backslash and quotes the string, so it can be pasted straight into code.

Worked examples

A price label. price: $9.99 (incl. tax) [EU] becomes:

price: \$9\.99 \(incl\. tax\) \[EU\]

As a constructor call: new RegExp("price: \\$9\\.99 \\(incl\\. tax\\) \\[EU\\]").

A file glob, slash not ticked. src/app/*.ts gives the pattern src/app/\*\.ts, which works in new RegExp("src/app/\\*\\.ts"). The literal form still escapes the slashes: /src\/app\/\*\.ts/.

Slashes and brackets. 1/2 (half) with the slash box ticked gives 1\/2 \(half\) and new RegExp("1\\/2 \\(half\\)").

Limits and tips

  • Hyphens stay as they are, so the pattern also works with the u and v flags. Inside a character class, escape a hyphen yourself.
  • Spaces and letters are not changed. The match is case-sensitive unless you add the i flag.
  • Escaping is not a full search tool. To match whole words, add \b around the escaped text yourself.
  • Glob wildcards become literal. *.ts matches only the text *.ts; write .*\.ts if you want a wildcard.

Frequently asked questions

Why does the RegExp version have double backslashes?
In a normal JavaScript string a single backslash starts an escape such as \n, so it must be written twice to reach the regex. The /…/ literal form is read by the regex engine directly and needs only one.
Should I tick "Also escape /"?
Tick it if the pattern goes between slashes in a regex literal, where an unescaped / would end the pattern. For new RegExp() it is not needed but does no harm. The regex literal output always escapes / either way.
Does the escaped pattern work with the u or v flag?
Yes. The hyphen is left as it is, because it only has a meaning inside square brackets and a \- outside them is an "Invalid escape" error with those flags. Every escape the tool adds is valid with u and v.
Is there a built-in function for this?
Newer engines have RegExp.escape(), which escapes more characters, including leading letters and digits. If you must support older browsers, this tool or a one-line replace does the job.
Do I need to escape text inside a character class?
Partly. Inside [ ] only ], \, ^ and - are special. The first three are escaped, but a hyphen is not, so put it first or last in the class, or escape it yourself, to keep it from making a range.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close