Character Classes in JavaScript

From the JavaScript Regular Expressions cheat sheet · Basic Patterns · verified Jul 2026

Character Classes

Matching specific sets of characters

javascript
.       Any character except newline
\d      Digit (0-9)
\D      Not a digit
\w      Word character (a-z, A-Z, 0-9, _)
\W      Not a word character
\s      Whitespace (space, tab, newline)
\S      Not whitespace
[abc]   Any of a, b, or c
[^abc]  Not a, b, or c
[a-z]   Character range a to z
[A-Z]   Character range A to Z
[0-9]   Any digit (same as \d)

// Examples
/\d{3}-\d{4}/.test("555-1234")  // true (phone)
/[aeiou]/.test("hello")         // true (vowel)
/[^0-9]/.test("abc")           // true (non-digit)
💡 Dot (.) matches any character except newline - use [\s\S] for all
⚡ Character classes can be negated with ^ inside brackets
📌 \w includes underscore (_) in word characters
🟢 Combine classes: [a-zA-Z0-9] for alphanumeric
character-classesbasics

More JavaScript tasks

Back to the full JavaScript Regular Expressions cheat sheet