Introduction
Regular expressions are powerful but unforgiving. If you need to match a literal string that happens to contain dots, asterisks, or parentheses, your regex will silently match the wrong things. A period in a regex matches any character. An unescaped parenthesis creates a capture group. The fix is simple: prefix every regex metacharacter with a backslash. But doing this by hand is error-prone, especially for strings with mixed special characters. This Regex Escaper does it instantly. Paste your text, get the escaped version, and paste it into your regex pattern.
What this tool does
- Escapes all 12 regex special characters: . * + ? ^ $ { } ( ) | [ ] \
- Bidirectional: also unescapes previously escaped regex strings.
- Live conversion with no submit button.
- Swap button to reverse the direction and exchange input with output.
- Handles Unicode input including emoji and multi-byte characters.
How this tool works
The escaper uses a single regex replacement that matches the 12 ECMAScript metacharacters: dot, asterisk, plus, question mark, caret, dollar sign, curly braces, parentheses, pipe, square brackets, and backslash. Each matched character is prefixed with a backslash. This is the same approach used by JavaScript's String.replace and by libraries like lodash's _.escapeRegExp. The unescape mode reverses the operation by removing backslashes that precede metacharacters. Both operations run on every keystroke. The tool does not validate or compile the resulting regex; it only handles the string escaping.
How regex escaping works
The regex metacharacters that have special meaning depend on context:
| Character | Meaning | Example | |---|---|---| | . | Any character (except newline) | "a.c" matches "abc", "a1c" | | * | Zero or more repetitions | "ab*" matches "a", "ab", "abbb" | | + | One or more repetitions | "ab+" matches "ab", "abbb" | | ? | Zero or one (optional) | "colou?r" matches "color", "colour" | | ^ | Start of string | "^abc" matches only at start | | $ | End of string | "abc$" matches only at end | | {n,m} | Repetition count | "a{2,4}" matches "aa" to "aaaa" | | ( ) | Capture group | "(abc)" captures "abc" | | | | Alternation (OR) | "cat|dog" matches either | | [ ] | Character class | "[a-z]" matches any lowercase | | \ | Escape character | "\." matches a literal period |
The ECMAScript specification defines these as PatternSyntax characters. When you want to match them literally, each must be escaped with a backslash. For example, to match the string "price: $9.99", you need the regex "price: \$9\.99".
This tool implements the standard escaping algorithm described in MDN's RegExp documentation.
How to use this tool
- Paste the literal text you want to match in a regex.
- The escaped version appears instantly in the output panel.
- Copy the escaped string and paste it into your regex pattern.
- To reverse, click Swap to switch to unescape mode and paste an escaped string.
Real-world examples
Matching a file path in a log parser
A developer writes a regex to match log lines containing the file path "/var/log/app.log". The dots in the path are regex metacharacters. They paste "/var/log/app.log" into the escaper and get "/var/log/app\.log". Now the regex matches the literal path instead of "/var/log/appXlog".
Searching for a price string
A data analyst needs to find all occurrences of "$19.99" in a text file. The dollar sign and period are both metacharacters. They paste "$19.99" and get "\$19\.99". Using this in their grep pattern finds exact matches without false positives from regex interpretation.
Building a URL pattern
A backend developer creates a route pattern that matches "api/v2/users.json". The dot before "json" would match any character. They escape it to "api/v2/users\.json" and use it in their Express.js route validation regex. The pattern now matches only the literal ".json" extension.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| This escaper | Low | Quick escaping of literal strings for regex patterns |
| lodash _.escapeRegExp | Low | Programmatic escaping in JavaScript code |
| Manual escaping | Low | One-off cases, error-prone for long strings |
Limitations or considerations
The escaper handles the 12 standard ECMAScript regex metacharacters. It does not escape characters that are only special inside character classes (like hyphens inside [ ]). If you are building a character class like [a-z.], the dot inside brackets does not need escaping. The tool also does not handle regex flags or Unicode property escapes (\p{...}). For complex regex construction, use a dedicated regex builder or tester tool.
Frequently asked questions
Which characters does it escape?
It escapes these 12 characters: . * + ? ^ $ { } ( ) | [ ] \ -- these are the standard ECMAScript regex metacharacters.
Does it escape hyphens?
No. Hyphens are only special inside character classes [a-z], not in the main pattern. Escaping them outside brackets is unnecessary.
Can I use the output directly in grep or sed?
Yes. The escaped output works in JavaScript, Python, Perl, grep -E, and most regex engines that follow the POSIX/ECMAScript syntax.
What is the difference between escape and unescape?
Escape adds backslashes before metacharacters. Unescape removes them. Use Swap to toggle between the two modes.
Does it handle Unicode?
Yes. Non-ASCII characters like emoji or accented letters pass through unchanged. Only the 12 ASCII metacharacters are escaped.
Conclusion
This tool removes a class of regex bugs that are hard to spot: silent false matches from unescaped metacharacters. Use it whenever you embed literal text in a regex pattern. For testing your regex patterns against sample input, combine the escaped output with a regex tester. For string transformation, try the String Case Converter.