Regex Escape Tool

Paste text to escape its regex special characters.

How it is calculated

The tool walks through your text one byte at a time and puts a backslash in front of every character from this fixed list: . ^ $ * + ? ( ) [ ] { } | \ /. Everything else is copied unchanged, including letters, digits, spaces, accented letters and emoji. The slash is on the list even though it is only special inside JavaScript regex literals. The hyphen - and the hash # are not escaped, so a hyphen inside a character class such as [a-z] still keeps its range meaning. The output also reports how many characters were escaped and the length of the original text counted in characters, not bytes. Empty text shows no result, while text made only of spaces is returned as it is. The result is intended for PHP, Python, JavaScript and similar engines, but check the flavour you use.

Worked example

Take the text price: $19.99 (USD) [net]. It has 25 characters. Six of them are metacharacters: the dollar sign, the full stop, the two round brackets and the two square brackets. Each gets a backslash in front, the letters, digits, colon and spaces are left alone, and the result is price: \$19\.99 \(USD\) \[net\]. The tool reports 6 characters escaped out of 25 characters. Used as a pattern, it now matches the original text literally instead of treating the dot as "any character" and the brackets as groups.

Try this example →

Common mistakes

  • Assuming the hyphen is escaped. It is not, so if you place the output inside a character class such as [...], escape any hyphen yourself or put it first or last.
  • Escaping the same text twice. Running the output through the tool again doubles every backslash, which then matches a literal backslash followed by the character.
  • Pasting the result into a language string without doubling the backslashes. In a normal Java, C or JavaScript string literal, \. must be written \\. unless you use a raw string.
  • Using the output for the replacement side of a find-and-replace. Only the pattern side needs this escaping; replacement strings follow different rules (such as $1).
  • Expecting whitespace, newlines or the hash to be treated specially. They pass through unchanged, so in free-spacing mode (where spaces and # have meaning) you must escape them yourself.

About this calculator

Regular expressions treat certain characters as metacharacters with special meaning — a period matches any character, an asterisk means zero or more, parentheses group patterns, and so on. If you want to search for those characters literally (a period in a filename, a dollar sign in a price), you have to escape each one with a backslash so the regex engine treats it as plain text instead of an instruction.

This tool automatically prefixes every metacharacter in your input — including the period, asterisk, plus, question mark, parentheses, brackets, braces, pipe, caret, dollar sign, and backslash itself — with a backslash, producing a string safe to drop into any regex pattern. It's equivalent to what functions like JavaScript's RegExp.escape, Python's re.escape, or PHP's preg_quote do programmatically.

Developers reach for this when building a regex dynamically from user input — like a search box — where an unescaped special character would either throw a syntax error or silently match the wrong things.

Was this helpful?

You can pick up to 3 reactions.

Comments

  • Be the first to comment.

Popular calculators

All Calculators