Back to JavaScript Notes
Topic #345

Character Classes

You will be able to use JavaScript regular expression character classes to match one character from a set, a range, or a shorthand group such as digits or word characters.

What it is

A character class is written inside square brackets, such as [aeiou]. It matches exactly one character from the allowed set. A range like [a-z] means every lowercase letter from a to z. A leading caret, [^aeiou], negates the class and matches one character that is not in the set. JavaScript also provides shorthand classes: \d for digits, \w for word characters, and \s for whitespace. Related terms include quantifiers, flags, and alternation.

Why it matters

  • Validate user input, such as checking whether a field contains only letters.
  • Extract specific characters, such as vowels, digits, or punctuation.
  • Replace unwanted characters, such as removing non-alphanumeric text.
  • Build compact patterns instead of listing many alternatives.
  • Support case-insensitive matching when combined with the i flag.

Syntax or steps

The smallest useful pattern is:

/[aeiou]/g

Read it as: match one character from the set a, e, i, o, or u, and do it globally.

  • [abc] matches one of a, b, or c.
  • [a-z] matches one lowercase letter.
  • [A-Za-z0-9] matches one alphanumeric character.
  • [^0-9] matches one non-digit character.
  • \d, \w, and \s are shorthand classes.

Inside a character class, most symbols are literal, but ^, -, ], and \ have special meaning. Put - first or last, or escape it, if you want a literal hyphen.

Example

const text = "Hello, World! 2024";

const vowels = /[aeiou]/g;
const digits = /\d/g;
const nonAlphanumeric = /[^A-Za-z0-9]/g;

console.log(text.match(vowels));
console.log(text.match(digits));
console.log(text.match(nonAlphanumeric));

Expected output:

[ 'e', 'o', 'o' ]
[ '2', '0', '2', '4' ]
[ ',', ' ', '!', ' ' ]

Part by part:

  • /[aeiou]/g finds each lowercase vowel. The g flag returns all matches, not just the first.
  • /\d/g finds each digit.
  • /[^A-Za-z0-9]/g finds each character that is not a letter or digit.
  • text.match(...) returns an array of matched characters, or null if there are no matches.

Common mistakes

  • Forgetting the g flag when you need every match. Without g, match returns only the first match.
  • Using [a-z] when uppercase letters should also match. Fix it by using [A-Za-z] or adding the i flag.
  • Placing - in the middle of a class accidentally, such as [a-z-0-9]. If you want a literal hyphen, put it at the end: [a-z0-9-].
  • Thinking a class matches multiple characters. [aeiou] matches one vowel; use a quantifier like [aeiou]+ for one or more.

When to use it

Use a character class when you need to match one character from a set or range. Use alternation when you need to match whole words or multi-character options.

NeedUseExample
One character from a setCharacter class/[aeiou]/g
One of several wordsAlternation/cat|dog|bird/g
Any digitShorthand class/\d/g
Simple literal checkString methodtext.includes("cat")

Practice

Guided exercise: write a pattern that matches every lowercase consonant in "JavaScript".

const word = "JavaScript";
const consonants = /[bcdfghjklmnpqrstvwxyz]/g;
console.log(word.match(consonants));

Expected output:

[ 'v', 'c', 'r', 'p', 't' ]

Challenge: extract all word-like tokens from "user_42, email@site.com". Hint: use \w+. Expected output: [ 'user_42', 'email', 'site', 'com' ].

Quick check

What does /[^0-9]/g match?

It matches every character that is not a digit.

Summary

Character classes let you describe a single allowed character compactly using sets, ranges, negation, or shorthand groups. They are useful for validation, extraction, and replacement, especially when combined with flags and quantifiers.

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

Character Classes – FAQs

Quick answers about learning Character Classes in JavaScript.

This free note from Coding Now Tech Institute explains Character Classes 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 Character Classes, is 100% free with no signup required.
With focused practice, most students grasp Character Classes 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