Literals and metacharacters
Literal characters match themselves; the twelve metacharacters control the engine. Learn which is which and how to escape when you need the literal character.
- Distinguish literal characters from metacharacters in a pattern
- Name all twelve metacharacters and their broad purpose
- Escape a metacharacter to match it literally
Every character in a regex pattern is either a literal or a metacharacter.
Literals match themselves — /hello/ matches the five-letter sequence hello.
Metacharacters have special meaning and control how the engine behaves rather than
matching a particular character.
The twelve metacharacters
. ^ $ * + ? { } [ ] \ | ( )That's the complete list. Any other character — letters, digits, most punctuation — is a literal. Here is what each metacharacter does at a glance:
| Character | Role |
|---|---|
. | Matches any single character except a newline |
^ | Start-of-string anchor (or negation inside […]) |
$ | End-of-string anchor |
* | Quantifier: 0 or more of the preceding atom |
+ | Quantifier: 1 or more of the preceding atom |
? | Quantifier: 0 or 1 of the preceding atom; also makes other quantifiers lazy |
{ } | Quantifier delimiters: {3}, {2,5} |
[ ] | Character class delimiters |
\ | Escape the next character, or start a shorthand like \d |
| | Alternation: match either the expression to the left or the right |
( ) | Grouping and capturing |
You will learn each one in context over the coming lessons. For now, the key thing to internalize is that these twelve characters are not literal: if you write them in a pattern you are giving an instruction, not asking for a literal match.
Literal matching
Everything else in a pattern is a literal. The pattern /price/ matches exactly
the five characters p, r, i, c, e in that order:
/price/.test("the price is right"); // true
/price/.test("the PRICE is right"); // false — case-sensitive by default
/price/.test("priceless"); // true — match anywhere in the stringLiteral matching is always case-sensitive unless you add the i flag (covered
later). It also matches anywhere in the string by default — the pattern does not
need to appear at the start or end unless you use anchors (^ and $).
Escaping metacharacters
To match a metacharacter literally, prefix it with a backslash \.
// Match a literal period (not "any character"):
/3\.14/.test("3.14"); // true
/3\.14/.test("3X14"); // false — escaped dot is literal
// Match a literal dollar sign:
/\$\d+/.test("$42"); // true
// Match a literal opening parenthesis:
/\(see note\)/.test("(see note)"); // trueWithout the escape, . would match any character and $ would be an anchor.
A common mistake: trying to match a file path like C:\Users\name and
forgetting that \U and \n might be interpreted as escape sequences. In a
regex, \n is a newline. Always check whether your backslash is introducing a
shorthand (\d, \w, \s) or escaping the next character.
The backslash itself
To match a literal backslash, you need \\ in the regex. In a JavaScript regex
literal this looks like:
/C:\\Users/.test("C:\\Users\\name"); // trueIn a language string (rather than a regex literal), the string layer eats one
backslash too, so you need four: "C:\\\\Users" becomes the regex C:\\Users.
JavaScript regex literals skip the string layer, which is why they're usually
cleaner for patterns with backslashes.
Putting it together
You will notice the double backslash \\. in the JavaScript code above. Inside
a template literal or regular string, \\ produces a single backslash
character, which the regex engine then sees as the escape \.. In a JavaScript
regex literal like /3\.14/ you write just one backslash — the slash delimiters
mean the engine reads it directly without a string parsing step.
Check your understanding
Knowledge check
- 1.Which of these are metacharacters in regex (not literal)?
- 2.You want to match a literal period in a URL like "example.com". Which pattern is correct?
Where to go next
Next: character classes — matching any character from a defined set, and the
handy shorthands like \d, \w, and \s.