Escape / Unescape
Regex Escape — Escape Special Characters for a Literal Match
Turn any literal text into a safe regex fragment — five flavors, so the escaping matches the engine that will actually compile the pattern.
In short
Escape regex metacharacters online — dot, parentheses, brackets, backslash, slash, dash — for JavaScript (RegExp.escape), Python re.escape, PHP preg_quote and Java \Q…\E.
By NaveenKumar T · Updated
- Escape & unescape in one tool
- Handles every special character
- Instant as you switch modes
Runs entirely in your browser — nothing you paste or open here is uploaded, logged, or stored.How we handle data →
Example
Regex Escape: input and output
A price string loaded with regex metacharacters — parentheses, a dollar sign, a dot, a slash, brackets, a question mark and a plus — the exact shape of a literal you'd want matched exactly, not interpreted.
price (USD): $12.50/unit [approx.] — 2+2=4? C:\temp\*.logprice \(USD\): \$12\.50\/unit \[approx\.\] — 2\+2=4\? C:\\temp\\\*\.logEvery metacharacter — ( ) $ . / [ ] ? + \ * — gained a backslash; the letters, digits, the colon, the em dash and the equals sign needed nothing, because none of them means anything special to a JavaScript regex outside a character class.
Learn more
Regex escaping rules
A regex engine reads certain characters as instructions rather than literals: . means "any character," ( and ) group, [ and ] open a character class, and so on. Escaping a plain string before it becomes part of a larger pattern — a user's search term dropped into a bigger regex, a literal filename that happens to contain dots and brackets — means backslash-prefixing every character the engine would otherwise treat as special, so price (USD): $12.50 stays exactly that string instead of becoming a broken (or dangerously permissive) pattern fragment.
The five flavors here exist because engines don't all treat the same set as special. JavaScript's own new RegExp.escape (the ES2025 standard-library function) does more than backslash-prefix metacharacters: it also hex-escapes a leading ASCII letter or digit — so the result is safe to paste even where it might be mistaken for the start of a token like \b — and hex-escapes several punctuation characters and whitespace that aren't regex-special but ARE meaningful elsewhere (in a replacement string, inside certain Unicode property escapes). The classic MDN-documented set most JavaScript code has used for a decade is narrower and purely about regex metacharacters. Python's re.escape narrowed its own set in 3.7 to stop over-escaping letters and digits; PHP's preg_quote needs the delimiter character (here, /) included in its set because preg_quote doesn't know which delimiter you'll wrap the pattern in; and Java's Pattern.quote doesn't escape character-by-character at all — it wraps the whole string in \Q…\E, a "treat everything between these markers as literal" instruction, which is why an embedded \E has to be spliced out rather than escaped.
Regex metacharacters and how each flavor escapes them
Five engines, one job — turn a literal character into something the regex compiler won't misread. Where a cell says "no escape", the character has no special meaning in that flavor's context and is passed through unchanged.
| Character | JavaScript (classic) | JavaScript (RegExp.escape) | Python re.escape | PHP preg_quote('/') | Java Pattern.quote |
|---|---|---|---|---|---|
| . | \. | \. | \. | \. | wrapped in \Q…\E |
| ( ) | \( \) | \( \) | \( \) | \( \) | wrapped |
| [ ] | \[ \] | \[ \] | \[ \] | no escape (only [ ) | wrapped |
| { } | \{ \} | \{ \} | \{ \} | \{ \} | wrapped |
| \ | \\ | \\ | \\ | \\ | wrapped |
| / | \/ | \/ | no escape | \/ | wrapped |
| - | no escape | no escape | \- | \- | wrapped |
| $ | \$ | \$ | \$ | \$ | wrapped |
| ^ | \^ | \^ | \^ | \^ | wrapped |
| | | \| | \| | \| | \| | wrapped |
| ? * + | \? \* \+ | \? \* \+ | \? \* \+ | \? \* \+ | wrapped |
| space | no escape | \x20 | \ (escaped) | no escape | wrapped |
| # | no escape | \x23 | \# (escaped) | \# | wrapped |
Escaping a pattern vs testing one
This tool answers one narrow question — "how do I make this literal text safe inside a regex?" — for text you already have. It is not the place to build or debug the pattern that CONTAINS that text: for that, the Regex Tester runs a live pattern against sample input with capture groups, flags and a replace preview, and the Regex Cheat Sheet is the reference for the syntax itself. A common pairing: escape a user-supplied search term here, splice it into a larger hand-written pattern, then verify the combined pattern in the tester before it ships.
Help
Frequently asked questions
Everything you need to know about the Regex Escape.
01How do I escape or unescape Regex here?
Pick the flavor of the engine that will compile the pattern, paste the literal text you want matched exactly, and press Escape. Drop the result into a larger pattern or a character class — it is a pattern fragment, with no surrounding delimiters added.
02How do I escape a dot, parentheses, brackets, a backslash, a slash or a dash in a regex?
For the common JavaScript case: a dot, parentheses ( ), brackets [ ], braces { }, a backslash, and a forward slash all get a backslash prefix under the classic flavor — . becomes \., ( becomes \(, and so on — because each one means something to the engine outside a character class. A literal dash - only needs escaping inside a character class, and only when it isn't first or last (a-z is a range, but [-az] or [az-] is a literal dash); outside a class, a bare - is never special and this tool leaves it alone. The full table below covers all five flavors side by side, since PHP and Python draw slightly different lines.
03Why does RegExp.escape write a leading letter as \x61?
Because the ES2025 RegExp.escape function is designed to be safe to CONCATENATE, not just safe to compile on its own — if the escaped text is spliced directly after something like \p{...} or a backreference number, an unescaped leading letter or digit could be read as part of that adjacent token instead of as the start of your literal. Hex-escaping the first character when it's an ASCII letter or digit removes that ambiguity entirely, at the cost of one two-character escape you'll only ever notice at the very start of the output. The classic escaping style most existing JavaScript code uses doesn't do this, which is why the two flavors can disagree on the very first character of otherwise-identical output.
04Which characters does Python's re.escape escape since 3.7?
()[]{}?*+-|^$\.&~# and the five whitespace characters space, \t, \n, \r, \v, \f. Before Python 3.7, re.escape backslash-prefixed every non-alphanumeric character, which produced needlessly noisy patterns for ordinary punctuation like a comma or an apostrophe; 3.7 trimmed the set down to exactly the characters that are actually special in Python's re syntax (plus whitespace, since re.VERBOSE mode treats unescaped whitespace as insignificant). Letters, digits and underscore were never escaped in either version.
05What do \Q and \E do in Java and PCRE?
\Q begins a literal run and \E ends it — every character in between, including ones that would otherwise be metacharacters, is matched exactly as written, with no per-character escaping needed. Java's Pattern.quote(s) is literally implemented as \Q + s + \E, and PCRE (used by PHP's preg_match_all, when you write the delimiters yourself, and many other engines) supports the same \Q...\E pair inside a pattern you write by hand. The one trap: if the literal text itself contains \E, it would prematurely end the quoted run, so a correct implementation (this tool included) splices it as \E\\E\Q — close the quote, match a literal \E the hard way, reopen the quote — rather than searching-and-replacing it away.
06Do I escape differently inside a character class?
Yes, and this tool escapes for use OUTSIDE a character class (the far more common case — dropping literal text into the body of a pattern). Inside [...], the rules shrink: ] needs escaping (or must come first), a literal backslash still needs escaping, ^ needs escaping only if it's the very first character (otherwise it means negation), and - needs escaping unless it's first or last (otherwise it defines a range like a-z). Everything else that's special outside a class — . ( ) { } | ? * + — is just a literal character inside one and needs no escaping at all. If you're building a character class from a literal set of characters, escape only ] \ ^ - by hand rather than pasting this tool's output verbatim.
Keep working
Related tools
Regex Tester
Type a pattern, watch matches light up in your text — with named capture groups, a replace preview, and flag toggles that behave exactly like JavaScript's RegExp.
JavaScript Escape
Escape / Unescape
Python Escape
Escape / Unescape
SQL Escape
Escape / Unescape
CSV Escape
Escape / Unescape
HTML Escape
Escape / Unescape