FormatterHQ

Regex Escape

Turn any literal text — prices, file paths, URLs, code — into a pattern that matches it exactly, escaped for JavaScript, PCRE/PHP, Python or a Java string literal.

Input TEXT
Output TEXT
Ready.

How to use the Regex Escape

  1. Paste the literal text you want to match into the input.
  2. Choose the Flavor: JavaScript, PCRE / PHP, Python or Java string literal.
  3. Tick Each line is a separate literal to escape a list of words and join them with |.
  4. Optionally tick Wrap in /…/ to get a ready JavaScript regex literal, then copy the output.

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 use u.
  • Escaping in code instead. PHP has preg_quote() and Python has re.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.

Frequently asked questions

Which characters need escaping in a regular expression?
The metacharacters are . * + ? ^ $ { } ( ) | [ ] \. In a JavaScript literal the forward slash must be escaped as well. This tool also escapes the hyphen so the result stays literal if you place it inside a character class.
Why does Java need double backslashes?
Java string literals use the backslash as their own escape character, so the regex \. has to be written as "\\." in source code. The Java option does that doubling for you.
Is this the same as PHP preg_quote or Python re.escape?
It escapes the same kind of characters, so the result matches the same literal text. The exact set can differ slightly between versions, but an extra backslash before a punctuation character is harmless in PCRE and Python.
Can I escape a list of words into one pattern?
Yes. Put one word or phrase per line and enable Each line is a separate literal. Each line is escaped and the lines are joined with |, for example foo\.js|bar\(\).

Last updated October 3, 2026

Guides

Related tools

Random Data from Regex Generate random strings that match a regular expression — ideal for test data such as IDs, codes and formats. Regex Cheat Sheet Interactive regex cheat sheet: anchors, classes, quantifiers, groups, lookarounds, flags and Unicode, with clickable examples and a flavour table. Regex Explainer Break a regular expression into tokens and explain each part in plain English: groups, quantifiers, classes, anchors and lookarounds. Regex Patterns Library Copy-ready regex patterns for email, phone, URL, IP, dates, numbers, UUIDs, postal codes and passwords, each with examples and a live test box. Regex Tester Test JavaScript regular expressions live: highlight matches, inspect capture groups and named groups, try flags and preview replacements. CSV Escape CSV escape online: convert special characters into escape sequences so text is safe inside CSV. SemVer Checker & Range Tester Validate, compare and sort semantic versions and test them against npm-style ranges like ^1.2.0, ~2.3 or >=1.0 <2. Snowflake ID Decoder Convert Twitter/X, Discord, Instagram and other Snowflake IDs to timestamps and show worker, process and sequence bits. Time Sheet Calculator Add up weekly work hours from start and end times with breaks. Calculates overtime per day or week and optional gross pay. TOTP / 2FA Code Generator Generate live TOTP 2FA codes from a Base32 secret or otpauth:// URI, create new secrets and QR codes for authenticator apps. ULID Decoder Decode ULIDs to their embedded timestamp and random part, and convert between ULID and UUID formats. Un-Google Link Remove Google redirect wrappers (google.com/url, AMP, image results) and tracking parameters to get the real destination URL.