SyntaxStudy
Sign Up
JavaScript Intermediate 5 min read

Character Classes in Regex

Character Classes

Character classes match one character from a defined set. Square brackets [...] define a custom class; shorthand classes like \d, \w, \s match common types.

Example
/[aeiou]/i      // Any vowel
/[a-z]/         // Lowercase letter
/[A-Za-z0-9]/  // Alphanumeric
/[^0-9]/        // NOT a digit (negated class)

// Shorthand classes
/\d/    // Digit [0-9]
/\D/    // Non-digit
/\w/    // Word char [A-Za-z0-9_]
/\W/    // Non-word char
/\s/    // Whitespace
/\S/    // Non-whitespace
Pro Tip

Negated classes [^...] match any character NOT in the set — useful for validating allowed character sets.