By the end of this lesson, you will be able to use JavaScript regular expression metacharacters such as ., \d, \w, and \s to match common text patterns without listing every possible character.
What it is
A metacharacter is a special symbol inside a regular expression that represents a class of characters or a position in text. Instead of writing [0123456789], you can write \d to mean “any digit.” In JavaScript, metacharacters are used inside regex literals like /\d+/ or inside new RegExp("\\d+").
Related terms include pattern, flags, character class, quantifier, and escape. A quantifier such as + says “one or more,” while an escape such as \. turns a metacharacter into a literal character.
Why it matters
- It makes patterns shorter and easier to read:
\d{3}is clearer than[0-9][0-9][0-9]. - It helps validate input such as phone numbers, dates, IDs, and email-like strings.
- It supports searching and replacing text in forms, logs, and user-generated content.
- It reduces mistakes when matching groups of characters, such as whitespace or word boundaries.
- It works with common JavaScript methods like
String.prototype.match,String.prototype.replace,String.prototype.search, andRegExp.prototype.test.
Syntax or steps
The smallest useful pattern is a regex literal between slashes:
/\d+/
Read it as: \d means a digit, and + means one or more digits. To use it, call a string method:
const text = "abc 123";
const result = text.match(/\d+/);
Common JavaScript metacharacters:
| Metacharacter | Meaning | Example |
|---|---|---|
. | Any single character except line terminators | /a.c/ matches abc |
\d | Any digit | /\d/ matches 7 |
\w | Any word character: letter, digit, or underscore | /\w+/ matches user_1 |
\s | Any whitespace character | /a\s+b/ matches a b |
\b | Word boundary | /\bcat\b/ matches cat but not concat |
^ | Start of input | /^\d/ matches a digit at the beginning |
$ | End of input | /\d$/ matches a digit at the end |
Example
const log = "User 42 logged in at 09:15";
console.log(log.match(/\d+/)[0]);
console.log(log.match(/\d{2}:\d{2}/)[0]);
console.log(log.match(/\w+/g));
Expected output:
42
09:15
[ 'User', '42', 'logged', 'in', 'at', '09', '15' ]
Part by part:
/\d+/finds the first run of digits, so it returns42./\d{2}:\d{2}/finds two digits, a literal colon, then two more digits, so it returns09:15./\w+/gfinds every run of word characters. Thegflag means “global,” so it returns all matches instead of only the first.
Common mistakes
- Forgetting to escape a literal dot.
/example.com/can matchexampleXcombecause.is a metacharacter. Use/example\.com/to match a real dot. - Assuming
\wmeans letters only. It also matches digits and underscores, so/\w+/matchesuser_1. - Using a single backslash in
new RegExp. In a string,"\d"becomesd, so writenew RegExp("\\d+"). - Expecting
.to match a newline. By default,.does not match line terminators.
When to use it
Use metacharacters when you need a compact way to describe broad character categories. Use explicit character classes when you need to limit matches to an exact set.
| Approach | Best when | Example |
|---|---|---|
| Metacharacters | Matching common categories like digits, words, or whitespace | /\d{3}-\d{4}/ |
| Explicit character class | Matching only selected characters | /[ABC][0-9]/ |
| Escaped literal | Matching punctuation that is normally special | /price:\$10/ |
Practice
Guided exercise: Write a pattern that matches a five-digit ZIP code in "Mail to 90210".
const address = "Mail to 90210";
console.log(address.match(/\d{5}/)[0]);
Expected output: 90210.
Challenge: Write a pattern that matches a word followed by whitespace and then a digit, such as "item 3".
Solution hint: start with \w+, add \s, then add \d.
Quick check
Question: What does /\s/ match?
Answer: It matches one whitespace character, such as a space, tab, or newline.
Summary
Metacharacters let JavaScript regular expressions describe patterns instead of exact characters. Learn the core symbols ., \d, \w, and \s, then use escapes and quantifiers to build precise, readable matching rules.