CHARACTER CLASSES
Shorthand classes
\d \w \s - the sets you use so often they got names.
Three sets appear so often they have one-letter names. Uppercase flips each one to its negation.
\d a digit, same as [0-9]. In "x9" it matches the 9.\w a word character: a letter, digit or underscore. In "a_1!" it matches a, _ and 1, not the bang.\s a whitespace character: space, tab or line break. In "a b" it matches the space.\D \W \S the negations: any one character that is NOT a digit / word char / whitespace. \D in "x9" matches the x.These work everywhere, including inside sets: [\d-] matches a digit or a dash.
\w and \d are ASCII-only: "naive" matches but "naïve" loses the ï, and other scripts miss entirely. For real Unicode add the u flag and use property escapes - \p{L} is any letter, \p{N} any number. MDN: Unicode property escapes.
PRACTICE - 2 DRILLS 0/2 DONE
Match every digit, using the shorthand instead of a bracket set.
/ /
order 66 shipped
must match: "6" "6"
item_4 of 9
must match: "4" "9"
no numbers here
must match nothing
Match every character that is NOT a digit, using the shorthand.
/ /
a1b2c3
must match: "a" "b" "c"
go4it
must match: "g" "o" "i" "t"
2024
must match nothing