Last updated
Regex Escape / Unescape Examples
The Regex Escape/Unescape tool adds or removes backslash escaping for special regex characters. Below are practical examples for common use cases.
Special Characters That Must Be Escaped
These characters have special meaning in regex and must be escaped to match literally:
. * + ? ^ $ { } [ ] | ( ) \
To match a literal dot: \.
To match a literal asterisk: \*
To match a literal plus: \+
To match a literal question: \?
To match a literal caret: \^
To match a literal dollar: \$
To match a literal pipe: \|
To match a literal backslash: \\
To match a literal open paren: \(
To match a literal close paren: \)
To match a literal open brace: \{
To match a literal open bracket:\[
Escaping a URL for Use in Regex
Escape a URL so it can be matched literally:
Input string:
https://example.com/path?query=1&page=2
Escaped regex pattern:
https://example\.com/path\?query=1&page=2
Matches exactly:
https://example.com/path?query=1&page=2
Without escaping, the dot would match any character and
the ? would make the preceding character optional.
Escaping a File Path
Escape a Windows file path for use in a regex:
Input string:
C:\Users\john\Documents\file.txt
Escaped regex pattern:
C:\\Users\\john\\Documents\\file\.txt
Each backslash becomes \\ and each dot becomes \.
Escaping User Input for Dynamic Regex
Safely use user-provided search terms in a regex:
User input: "price (USD)"
Without escaping — broken regex:
/price (USD)/ ← ( and ) create a capture group
With escaping — safe regex:
/price \(USD\)/ ← matches the literal string
JavaScript escaping function:
function escapeRegex(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
const userInput = 'price (USD)';
const safePattern = new RegExp(escapeRegex(userInput));
Python Escaping
Escape a string for use in a Python regex:
Input: "file.name (copy)"
Python re.escape() output:
file\.name\ \(copy\)
Usage:
import re
pattern = re.compile(re.escape("file.name (copy)"))
match = pattern.search(text)
JavaScript String Literal Double-Escaping
The double-escaping problem when writing regex as a string literal:
To match a literal backslash:
As a regex literal: /\\/
As a string literal: new RegExp("\\\\")
Explanation:
- String parser sees "\\\\" and produces "\\"
- Regex engine sees "\\" and matches one literal backslash
To match \d (literal backslash + d):
Regex literal: /\\d/
String literal: new RegExp("\\\\d")
Character Class Escaping Rules
Inside square brackets, different escaping rules apply:
Inside a character class []:
] must be escaped: \]
\ must be escaped: \\
^ must be escaped when first: \^
- must be escaped when between chars: \-
Characters that DON'T need escaping inside []:
. * + ? ( ) { } | $ ← safe to use literally
Example:
[.*+?] matches a literal dot, asterisk, plus, or question mark
[\.\*\+\?] same result but unnecessarily escaped
Unescaping a Complex Pattern
Unescape a regex to understand what it literally matches:
Escaped pattern:
/https?:\/\/[\w\-]+(\.[\w\-]+)+\/path\?id=\d+/
Unescaped (literal string it matches):
https:// or http:// + word chars/hyphens + (.word chars/hyphens)+ + /path?id= + digits
Readable breakdown:
https? → "http" or "https"
:\/\/ → "://"
[\w\-]+ → one or more word chars or hyphens (domain)
(\.[\w\-]+)+ → one or more ".something" segments
\/path → "/path"
\?id= → "?id="
\d+ → one or more digits
Escaping for Different Contexts
The same string may need different escaping in different contexts:
String to match: "Hello (World)"
In a regex literal (JavaScript):
/Hello \(World\)/
In a string literal (JavaScript):
new RegExp("Hello \\(World\\)")
In a Python raw string:
re.compile(r"Hello \(World\)")
In a Python regular string:
re.compile("Hello \\(World\\)")
In a Java string:
Pattern.compile("Hello \\(World\\)")
In a shell command (grep):
grep "Hello \(World\)" file.txt
grep -E "Hello \(World\)" file.txt
Security — Preventing ReDoS
Escaping user input prevents catastrophic backtracking attacks:
Malicious input: "(a+)+"
Without escaping — vulnerable:
new RegExp("(a+)+")
Testing against "aaaaaaaaaaaaaaab" causes exponential backtracking
With escaping — safe:
new RegExp("\\(a\\+\\)\\+")
Matches the literal string "(a+)+" — no backtracking risk
- Escape any string to use it as a literal regex pattern
- Unescape a regex to read what it literally matches
- Handle double-escaping for JavaScript string literals
- Apply correct escaping rules inside character classes
- Generate escaping code for JavaScript, Python, Java, and PCRE
- Prevent ReDoS by escaping user input before regex use
- Escape file paths, URLs, and other strings with special characters