Yueseryueser.com

← All guides

Which Characters Need Escaping in a Regex? The 14-Character List

Search for 1.5, paste it straight into a regex, and the dot matches any character — so 1X5 and 1a5 come back too. Escaping puts a backslash before the characters that carry special meaning, so each one stands only for itself.

Open the tool:Escape Regex

When escaping is not optional

Search boxes, log filters and bulk renames all do the same thing: treat user input as literal text. The moment the input contains a dot, an asterisk, a bracket or a parenthesis, an unescaped pattern changes meaning — at best it matches a pile of unrelated lines, at worst it throws a syntax error.

The same applies to matching file names, URLs and version numbers. The dots in 1.2.3, the plus in a+b and the brackets in [draft] all need escaping to match literally, or 1.2.3 will happily match 1x2y3.

Which 14 characters, exactly

The list is: dot, asterisk, plus, question mark, caret, dollar sign, opening and closing curly brace, opening and closing parenthesis, pipe, opening and closing square bracket, and the backslash itself. Fourteen in total. Each has special meaning in a regex and becomes literal once a backslash precedes it.

Anything else is left alone: hyphens, slashes, spaces, letters and digits get no backslash. That keeps the output shorter and easier to read than escaping every character. If the text goes inside a character class, though, a hyphen in the middle still has meaning and has to be handled separately.

Two traps when pasting into a pattern

First, do not escape twice. The backslash is itself on the list, so escaping an already-escaped string doubles every backslash and the pattern then looks for a literal backslash followed by the character, which matches nothing you wanted.

Second, many languages add a string layer on top. Java needs two backslashes to produce one in the regex, and Python behaves the same unless you use a raw string. This tool handles the regex layer only; the language string layer is yours to add.

FAQ

Which characters need escaping?

Dot, asterisk, plus, question mark, caret, dollar sign, curly braces, parentheses, pipe, square brackets, and the backslash itself — 14 characters. Hyphens and slashes are not on the list, so the tool leaves them untouched.

Why is the backslash still there after escaping?

The backslash is the escape marker: it tells the engine the next character is literal, so it has to remain. When you write the result into code, add another layer if the language also processes backslashes — Java string literals need two, for instance.

Can I just use re.escape or RegExp.escape instead?

Yes, the purpose is the same: turn special characters into literals. The exact character set differs slightly between implementations, and some also escape spaces or hyphens, so compare actual output rather than assuming two languages behave identically.

Related tool:Escape Regex