Back to JavaScript Notes
Topic #343

Metacharacters

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, and RegExp.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:

MetacharacterMeaningExample
.Any single character except line terminators/a.c/ matches abc
\dAny digit/\d/ matches 7
\wAny word character: letter, digit, or underscore/\w+/ matches user_1
\sAny whitespace character/a\s+b/ matches a b
\bWord 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 returns 42.
  • /\d{2}:\d{2}/ finds two digits, a literal colon, then two more digits, so it returns 09:15.
  • /\w+/g finds every run of word characters. The g flag means “global,” so it returns all matches instead of only the first.

Common mistakes

  • Forgetting to escape a literal dot. /example.com/ can match exampleXcom because . is a metacharacter. Use /example\.com/ to match a real dot.
  • Assuming \w means letters only. It also matches digits and underscores, so /\w+/ matches user_1.
  • Using a single backslash in new RegExp. In a string, "\d" becomes d, so write new 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.

ApproachBest whenExample
MetacharactersMatching common categories like digits, words, or whitespace/\d{3}-\d{4}/
Explicit character classMatching only selected characters/[ABC][0-9]/
Escaped literalMatching 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.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Metacharacters – FAQs

Quick answers about learning Metacharacters in JavaScript.

This free note from Coding Now Tech Institute explains Metacharacters in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Metacharacters, is 100% free with no signup required.
With focused practice, most students grasp Metacharacters in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now