Regex escape: match text literally
A regex escape turns plain text into a pattern that matches only that text. Characters like ., $, ( and * have special meanings in regular expressions, so a search for $19.99 written as-is would never find the price. This tool puts a backslash in front of every metacharacter for the regex flavor you choose.
Example
Input text:
Price: $19.99 (incl. tax) [US]*
JavaScript, PCRE and Python output:
Price: \$19\.99 \(incl\. tax\) \[US\]\*
Java string literal output, with every backslash doubled so it can go straight between quotes in Pattern.compile("…"):
Price: \\$19\\.99 \\(incl\\. tax\\) \\[US\\]\\*
Options explained
- JavaScript escapes
. * + ? ^ $ { } ( ) | [ ] \ / -. The forward slash is escaped too, so the result is safe inside a/…/literal. - PCRE / PHP and Python escape the same set but leave
/alone and also escape#, which starts a comment when the extended (x) flag is on. - Java string literal produces the pattern and then doubles each backslash for a Java (or Kotlin, C#) string.
- Each line is a separate literal escapes every line on its own, drops empty lines and joins the rest with
|, which is handy for a list of keywords. - Wrap in /…/ adds the slashes for a JavaScript regex literal.
Line breaks and tabs in the input become \n, \r and \t. With line mode on, the three lines a.b, c+d and 1/2 become /a\.b|c\+d|1\/2/.
Common pitfalls
- Double escaping. If you paste the output into a JavaScript string for
new RegExp("…"), each backslash must be doubled. Use the Java option or run the result through the JavaScript string escaper. - The u or v flag. In Unicode mode, JavaScript only allows escapes of syntax characters and
/. An escaped hyphen (\-) outside a character class then throws "Invalid escape", so remove that backslash if you useu. - Escaping in code instead. PHP has
preg_quote()and Python hasre.escape(). Use this tool when you are writing a pattern by hand, in a config file or in a search box.
After escaping, paste the pattern into the regex tester to confirm it matches the text you expect.
Privacy
Escaping runs in your browser; the text you paste is not sent anywhere.