How to use the JS regex escaper
- Type or paste the exact text into Literal text to match.
- Keep Also escape / (for /…/ literals) ticked if the pattern will sit between slashes.
- Copy the version you need: Escaped pattern, As a regex literal or As a RegExp constructor call.
- 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
iflag. - Escaping is not a full search tool. To match whole words, add
\baround the escaped text yourself. - Glob wildcards become literal.
*.tsmatches only the text*.ts; write.*\.tsif you want a wildcard.
Frequently asked questions
Why does the RegExp version have double backslashes?
Should I tick "Also escape /"?
Does the escaped pattern work with the u or v flag?
Is there a built-in function for this?
Do I need to escape text inside a character class?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.