Regex has a small alphabet of characters that don’t mean what they look like. Type a period expecting it to match a period, and instead it matches any character. This lesson covers exactly which characters are special, and how to tell the engine “no, I mean this character literally.”
The metacharacters
These characters carry special meaning inside a regex pattern:
. * + ? ^ $ { } ( ) | [ ] \
Each one triggers different behavior — . matches any character, * and + are quantifiers, ^ and $ are anchors, () create groups, [] create character classes, | means alternation, and \ itself starts an escape sequence or a shorthand like \d. If you want the regex engine to treat any of these as a plain, ordinary character, you have to escape it by putting a backslash in front: \., \$, \(, \\, and so on.
"1.2.3".match(/\d\.\d\.\d/); // matches "1.2.3"
"1x2x3".match(/\d\.\d\.\d/); // null — the literal dot doesn't match "x"
Without the backslashes, v\d+.\d+.\d+ would still match "v1.2.3" — but it would also match "v1x2x3", "v1-2-3", or anything else with a character where the dots are. Escaping the dots makes the pattern say exactly what you mean: “a real, literal period,” not “any character here.”
Real-world cases
A literal dollar sign in a price. $ is the end-of-string anchor, so matching an actual dollar sign requires \$:
"Price: $19.99".match(/\$\d+\.\d{2}/); // matches "$19.99"
A literal slash in a URL. Slashes aren’t metacharacters in JavaScript’s /pattern/ syntax the way they are in some tools, but they do need escaping if you’re writing the pattern between two literal slashes, because the engine would otherwise read an unescaped / as the end of the pattern:
// Between /.../ delimiters, an unescaped / would end the pattern early
const re = /https?:\/\/[^\s]+/;
"visit https://example.com/path".match(re);
// matches "https://example.com/path"
If you build the same pattern with new RegExp("...") instead, you don’t need to escape /, because there are no slash delimiters to confuse it with — but you’d still escape the dot in example\.com if you cared about matching that domain exactly.
Escaping cheat list
| Character | Special meaning | Escaped form | Matches literally |
|---|---|---|---|
. |
Any character | \. |
A period |
* |
Zero or more | \* |
An asterisk |
+ |
One or more | \+ |
A plus sign |
? |
Zero or one / lazy modifier | \? |
A question mark |
$ |
End of string/line | \$ |
A dollar sign |
( ) |
Group | \( \) |
Parentheses |
[ ] |
Character class | \[ \] |
Square brackets |
\ |
Escape character | \\ |
A single backslash |
Tip
Not every special character needs escaping in every context. Inside a character class like [.+?], most metacharacters already lose their special meaning and match literally — [.] matches a literal dot without a backslash. The exceptions are ], \, ^ (at the start), and - (in the middle), which can still need escaping inside a class.
Try it yourself
Take the price demo above and remove the backslash before the $, so the pattern becomes $\d+\.\d{2}. Predict what happens before you test it.
What should happen
$ at the start of a pattern (outside a character class) is interpreted as the end-of-string anchor, so $\d+\.\d{2} would require the string to end right where it starts matching — which is a contradiction for anything with real content after it, so it simply won’t match. Escaping \$ is what turns it back into a plain, matchable dollar sign.
What’s next
You’ve now covered the vocabulary of regex — literals, classes, quantifiers, groups, anchors, lookaround, flags, and escaping. In the next lesson, you’ll put all of it together and walk through several complete, real-world patterns from start to finish.