Literal Characters & Escaping — Regex

Most characters are literal and match themselves. The 14 metacharacters (\ . ^ $ | ? * + ( ) [ ] { }) are read as instructions; put \ in front to find one as…

Sari wrote no. (short for number) only once in the chat. Yet chat.match(/no./g) finds three. The dot in that pattern isn't a plain dot after all: the regex reads it as an instruction.

Most characters in a regex pattern are literal characters: they match themselves. But 14 characters are read as instructions; they are called metacharacters. It's like the asterisks in WhatsApp: Mrs. Ratna types *Tart*, and the group sees the word Tart in bold. The asterisks don't show, because WhatsApp reads them as the instruction "make this bold". In a regex, when you want to find one of those instruction characters as it is, put a backslash \ in front of it. That is called escaping.

Literal characters and metacharacters

Letters, digits, spaces, -, @, :, , and most other punctuation are literal. /PO-0413/ looks for the text PO-0413 exactly. Only these 14 are read as instructions:

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

In a regex literal /.../, the slash / has to be escaped too, as \/, because a slash closes the pattern.

Escaping with a backslash

Four things in the chat that use metacharacters, written without and with escaping:

console.log(chat.match(/(Admin)/g));       // ['Admin', 'Admin']
console.log(chat.match(/\(Admin\)/g));     // ['(Admin)', '(Admin)']
console.log(chat.match(/much?/g));         // ['much']
console.log(chat.match(/much\?/g));        // ['much?']
console.log(chat.match(/\+44/g));          // ['+44']
console.log(chat.match(/\[12\/03/g).length); // 6

The slipperiest are the ones that don't throw: the pattern runs, but the result isn't what you meant.

The dot: matches any character

console.log(chat.match(/no./g));  // ['now', 'now', 'no.']
console.log(chat.match(/no\./g)); // ['no.']

An unescaped dot matches any one character. So /no./ matches now in Snowballs, now in snowballs, and only once the no. you wanted. That's 3 in total. With \., the dot only matches a real dot, and just 1 is left.

That's why /3.14/.test("3X14") is true too: the dot in the pattern matches the X. Write /3\.14/ when you mean the number 3.14.

There is one thing a dot doesn't match: a line break (\n). After PO-0412 on the first line, the chat breaks to a new line.

console.log(chat.match(/PO-0412./g));  // null
console.log(chat.match(/PO-0412./gs)); // ['PO-0412\n']

The s flag (dotAll) makes the dot match line breaks too.

Escaping inside a string: new RegExp()

Sometimes a pattern is built from a string, for example from what a user typed: new RegExp("...", "g"). Here the backslash has to be doubled, because the JavaScript string eats one backslash first.

console.log(new RegExp("no\.").source);  // no.
console.log(new RegExp("no\\.").source); // no\.

⚠️ A backslash too few, or one too many

TL;DR: Most characters are literal and match themselves. The 14 metacharacters (\ . ^ $ | ? * + ( ) [ ] { }) are read as instructions; put \ in front to find one as it is, and in a regex literal escape / too. A dot matches any one character except a line break (the s flag changes that), so /3.14/.test("3X14") is true and a real dot is written \.. Some mistakes give a SyntaxError (/+44/: Nothing to repeat), others are silently wrong (/(Admin)/, /much?/). In a string for new RegExp("..."), write the backslash twice.

⚡ 14 characters are instructions

Meaning: Most characters match themselves. Only the 14 metacharacters (\ . ^ $ | ? * + ( ) [ ] { }) are read as instructions. Put \ in front of one to find it as it is.

⚡ A dot means "any character"

Write:

console.log(chat.match(/no./g));
console.log(chat.match(/no\./g)); // ['no.']
Guess first: Sari wrote no. only once. How many does /no./g find?

Three. A dot without a backslash matches any one character, so the now in Snowballs and in snowballs come along too. A real dot is written \..

Meaning: /3.14/.test("3X14") is true too. When you mean a real dot, write \..

⚡ Some throw, some are silently wrong

Write:

console.log(chat.match(/much?/g));  // ['much']
console.log(chat.match(/much\?/g)); // ['much?']

Meaning: /+44/ throws at once (Nothing to repeat), but /much?/ runs and is silently wrong: ? makes the h optional.

⚡ In a string, two backslashes

Write:

console.log(new RegExp("no\\.").source); // no\.

Meaning: A JavaScript string eats one backslash first. In a regex literal /.../ write one backslash, in a string "..." write two.

What you will learn