TExcelTAD
Home › Regular Expressions › Metacharacters & Escaping

Metacharacters & Escaping

Some characters don't match themselves — they give instructions to the regex engine. These are the metacharacters:

.  ^  $  *  +  ?  (  )  [  ]  {  }  |  \

In a JavaScript regex literal the forward slash / also needs care, because it ends the literal.

The problem

Suppose you want to find the price 3.50. The dot is a metacharacter meaning "any character", so 3.50 also matches 3x50 and 3-50:

regex · try it yourself
/3.50/g
Prices: 3.50, 3x50, 3-50
3 matches
#1 "3.50" at index 8
#2 "3x50" at index 14
#3 "3-50" at index 20

The fix: escape with a backslash

Put a backslash \ in front of a metacharacter to make it literal. \. means "a real dot":

regex · try it yourself
/3\.50/g
Prices: 3.50, 3x50, 3-50
1 match
#1 "3.50" at index 8

The same goes for every other metacharacter:

To match Write
. \.
* \*
+ \+
? \?
( and ) \( and \)
[ and ] \[ and \]
{ and } \{ and \}
^ and $ \^ and \$
\ \\
/ (in a JS literal) \/

The vertical bar (alternation) is escaped the same way: a backslash followed by the bar matches a literal bar character.

regex · try it yourself
/\(\d+\) \$\d+\.\d\d\?/g
Questions: (1) $4.99? and (2) $10.00?
2 matches
#1 "(1) $4.99?" at index 11
#2 "(2) $10.00?" at index 26

Backslash also creates special sequences

A backslash in front of an ordinary letter usually does the opposite: it turns the letter into something special. \d is "a digit", \n is a newline, \t is a tab, \b is a word boundary. So a backslash means "flip the meaning": special becomes literal, and (some) letters become special.

Sequence Meaning
\d \w \s digit / word character / whitespace
\n \t \r newline / tab / carriage return
\b word boundary
\uXXXX the character with that hex code, e.g. \u00e9 = é

Tip: Escaping a punctuation character that is not special (like \- outside a class, or \@) is harmless in most engines, but with the u flag JavaScript rejects unnecessary escapes such as \@. Escape only what you need.

Matching a backslash and a slash

To match one backslash you write two: \\. To match a / inside a JavaScript literal, escape it as \/:

regex · try it yourself
/C:\\Users\\\w+|\/home\/\w+/g
Paths: C:\Users\ana and /home/bao
2 matches
#1 "C:\\Users\\ana" at index 7
#2 "/home/bao" at index 24

The output shows matches as JSON strings, which is why a single backslash is displayed as \\.

Escaping user input in code

When you build a regex from a variable (for example a search box), the user's text may contain metacharacters. Escape them first. Modern JavaScript has RegExp.escape in newer engines; this small helper works everywhere:

js · try it yourself
function escapeRegex(s) {
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\
amp;"); } const query = "1+1=2?"; const re = new RegExp(escapeRegex(query), "g"); console.log(re.source); console.log("Is 1+1=2? Yes, 1+1=2?".match(re));
1\+1=2\?
[ '1+1=2?', '1+1=2?' ]

amp; in the replacement means "the whole match", so every metacharacter gets a backslash in front of it. Python offers the same idea built in as re.escape().

python · try it yourself
import re

print(re.escape("price: $3.50 (approx)"))
price:\ \$3\.50\ \(approx\)

Test yourself

Which pattern matches only the literal text a+b?

  1. /a+b/
  2. /a\+b/
  3. /a\\+b/
  4. /[a+b]/

What does /1.5/ match in 1x5?

  1. Nothing
  2. 1x5
  3. 1
  4. It is a syntax error

How do you match a single backslash in a regex?

  1. \
  2. \\
  3. /\/
  4. [\]