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:
\ . ^ $ | ? * + ( ) [ ] { }
.means "any character". Covered below.\is the escape key.[ ]means "one character from a list": the Character Classes lesson.* + ? { }set how many times something repeats: the Quantifiers lesson.^ $mark the start and the end: the Anchors lesson.( )and|make groups and "or" choices: the lessons after that.
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
(Admin)without escaping doesn't throw, but the parentheses become a group and aren't searched for. OnlyAdminis found.much?without escaping doesn't throw either:?makes the letterhoptional, so the question mark isn't included./+44/throws straight away:SyntaxError: Invalid regular expression: /+44/: Nothing to repeat.+means "repeat what came before", and at the start of the pattern there is nothing to repeat.new RegExp("[12")throws too:Invalid regular expression: /[12/: Unterminated character class, because[opens a list that is never closed. An opening parenthesis with no closing one givesInvalid regular expression: /(/: Unterminated group.\[12\/03finds the date[12/03at the start of each line:\[for the square bracket,\/for the slash. It finds 6, one per line.
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\.
- In a string,
"no\."is the same as"no.".\.isn't a string escape, so the backslash quietly disappears, with no error. The regex receivesno.and finds 3 again. "\\"in a string becomes one\. So write"no\\."for the regex to receiveno\..- The easy rule: in a regex literal
/.../write one backslash, in a string"..."write two. A slash in a string needs no escaping:new RegExp("12/03")works. - When the string is a user's input, you don't need to add backslashes one by one:
RegExp.escape(input)(Node 24 and browsers since 2025) escapes every metacharacter at once. The lesson Regex in JavaScript shows how.
⚠️ A backslash too few, or one too many
- Forgetting to escape the dot.
/[email protected]/also matchesdinaXputri@exampleXcom. Write/dina\.putri@example\.com/. - Unescaped parentheses.
/(Admin)/doesn't throw, but the parentheses drop out of the result. Want the parentheses too?\(and\). - Escaping characters that aren't metacharacters.
/\@/still runs, but with theuflag it throws:Invalid regular expression: /\@/u: Invalid escape. Escape only the 14 characters, plus/in a regex literal. - A single backslash in a string.
new RegExp("3\.14")loses its backslash and the dot means "any character" again. Double it:"3\\.14".
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.