How to Unescape a Regex (Reading a\.b Out Loud)
A regex copied from someone else, or a search term stored escaped in a log, is hard to read: a\.b, \[test\]. Unescaping removes the extra backslashes so you can see what it was trying to match.
When to strip the backslashes
Inherited code and config files usually carry regexes pasted from somewhere else, complete with a thicket of backslashes. Unescaping to plain text is faster than converting each character in your head, and much less likely to get the levels wrong.
The other case is comparison. A log records the escaped search term while the user reports the original spelling, and until both look the same there is no way to tell whether they are the same word.
- Reading a regex found in code or config
- Recovering an escaped search term from a log
- Showing the source text behind a pattern in documentation
- Checking whether two patterns are equivalent
It only removes 14 backslashes
The scope is a fixed list: dot, asterisk, plus, question mark, caret, dollar sign, curly braces, parentheses, pipe, square brackets and the backslash. A backslash is removed only when it precedes one of those; everywhere else it stays.
That means \d, \w, \s and \n are untouched, since they are not a backslash plus one of those characters. Two backslashes do collapse into one, because the backslash is on the list itself.
- Only the 14 special characters are unescaped
- \d, \w and \n keep their backslash
- A doubled backslash becomes a single one
- Nothing else is modified
Why the result stops working as a pattern
Once the backslashes are gone, the characters they protected get their special meaning back. a\.b becomes a.b, where the dot now matches any character, so using it as a pattern matches things the original never did. Keep the original, or escape the result again, if you still need to match.
Paths and code also suffer. In something like C:\dir\*.txt the \* is treated as an unnecessary escape and removed, changing what the path means. Back up the source and compare before adopting the output.
- The output is not a working pattern
- Use it for reading, comparing and re-editing
- Backslashes in Windows paths get removed
- Keep the original for reference
FAQ
Can I still use the result as a regex?
No. Removing the backslashes restores special meaning to those characters, so the text matches different things than the original pattern did. Keep the original version for matching, or escape the result again before use.
Why are \d and \n unchanged?
Because they are not on the list. Only backslashes before the 14 special characters are removed, and the letters d, w, s and n are not among them. Sequences like \d are part of regex syntax rather than redundant escaping.
Can it decode \u4e2d style escapes?
No. That is a Unicode escape sequence, and u is not one of the 14 characters, so it survives unchanged. Decoding those requires a tool or language function that understands escape sequences rather than a plain backslash remover.
Related tool:Unescape Regex