Shorthand Classes \d \w \s
Regex provides shortcuts for common character sets. These are easier to type and read.
The Big Three
| Shorthand | Equivalent | Matches |
|---|---|---|
\d | [0-9] | Any digit |
\w | [a-zA-Z0-9_] | Word character (letter, digit, underscore) |
\s | [ \t\n\r] | Whitespace (space, tab, newline) |
Negated Shortcuts
Uppercase versions match the OPPOSITE:
| Shorthand | Equivalent | Matches |
|---|---|---|
\D | [^0-9] | NOT a digit |
\W | [^a-zA-Z0-9_] | NOT a word character |
\S | [^ \t\n\r] | NOT whitespace |
Digit Exercises
Word Character Exercises
Whitespace Exercises
Negated Class Exercises
Combining Shorthand Classes
Practice Playground
Try:
\d+- sequences of digits\w+- words\s+- whitespace groups\W- non-word characters (punctuation)
Quick Reference
| Pattern | Meaning |
|---|---|
\d | Digit [0-9] |
\D | Non-digit [^0-9] |
\w | Word char [a-zA-Z0-9_] |
\W | Non-word [^a-zA-Z0-9_] |
\s | Whitespace |
\S | Non-whitespace |
Key Takeaways
\d,\w,\sare shortcuts for common sets- Uppercase versions (
\D,\W,\S) match the opposite \wincludes underscore (useful for programming)- These work inside character sets too:
[\d\s]

