Shorthand classes
\d, \w and \s are named character classes, and \D, \W and \S are their opposites. See exactly what each matches and where flavours and locales disagree.
Shorthand classes
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- \d is a digit, \w is a word character and \s is whitespace. The capital letters \D, \W and \S mean everything else.
- In JavaScript \w is exactly [A-Za-z0-9_]. It does not match é, a hyphen or a space, so \w+ splits "café" into caf.
- In Python 3 a str pattern's \w matches é and 中, and \d matches Arabic-Indic digits. The re.ASCII flag restores the narrow meaning.
- Shorthands work inside brackets: [\w.-]+ is word characters, dots and hyphens. Put the dash last.
- POSIX classes like [[:alpha:]] are the portable spelling in grep and sed, but in JavaScript they silently mean something else. BSD sed -E does not know \d or \w at all.
Names for the classes you use all the time
Some classes come up in nearly every pattern: the digits, the characters you’d find in an identifier, the gaps between words. Writing [0-9] or [A-Za-z0-9_] over and over is tedious and invites typos. So regex gives them one-letter names, each introduced by a backslash:
\d: a digit.\w: a “word” character, meaning a letter, digit or underscore.\s: whitespace, meaning space, tab, newline and a few relatives.
The capital versions are the negations: \D is “not a digit”, \W is “not a word character” and \S is “not whitespace”. They behave like a negated class from the previous lesson, so \S matches punctuation and every other visible character, and \W matches the space and the hyphen.
"a1 b2".replace(/\D/g, "") // 12
"a b\tc".split(/\s+/) // [ 'a', 'b', 'c' ]
"tel: 555 0142".match(/\S+/g) // [ 'tel:', '555', '0142' ]
Because they are classes, they work inside square brackets too. To match a run of word characters and hyphens:
/[\w-]+/.exec("foo-bar baz")[0] // foo-bar
/[\w.-]+@[\w.-]+/.exec("mail ada.l@ex-ample.org now")[0] // ada.l@ex-ample.org
The dash goes last so it is a literal, as in the character classes lesson.
What “word character” really means
The names are friendly, but the definitions are strict. I ran every shorthand against ten characters in Node 26 (Y means the pattern matched):
char \w \d \s \W \D \S
a Y - - - Y Y
_ Y - - - Y Y
7 Y Y - - - Y
é - - - Y Y Y
٣ (Arabic-Indic 3) - - - Y Y Y
- - - - Y Y Y
space - - Y Y Y -
tab - - Y Y Y -
nbsp - - Y Y Y -
中 - - - Y Y Y
In JavaScript, \w is exactly [A-Za-z0-9_] and \d is exactly [0-9]. A “word” character does not include é, 中 or a hyphen. So "order 66: x_1 café".match(/\w+/g) returned ['order', '66', 'x_1', 'caf']: the é ended the word. The underscore is included, which is why x_1 stays whole. \s does cover the non-breaking space (U+00A0), which is easy to forget when text arrives from a web page.
Flavours disagree
Python 3 treats a string pattern’s shorthands as Unicode-aware by default:
'é' \w \d \s -> Y - - with re.ASCII -> - - -
'٣' \w \d \s -> Y Y - with re.ASCII -> - - -
'中' \w \d \s -> Y - - with re.ASCII -> - - -
'\xa0' \w \d \s -> - - Y with re.ASCII -> - - -
So re.findall(r"\w+", "café x_1") returned ['café', 'x_1'], and with re.A it returned ['caf', 'x_1'], the same as JavaScript. The same pattern gives different words depending on the language. JavaScript can opt in to Unicode with property escapes, which the later unicode lessons cover.
The command-line tools split further, and for é the answer depended on the locale and flags (1 means it matched):
grep -E UTF-8 grep -E LC_ALL=C pcre2grep perl (bytes) perl -Mutf8
\w 1 0 0 0 1
[[:alpha:]] 1 0 0 0 1
\d 0 0 0 0 0
BSD grep -E matched é with \w only under LC_ALL=C.UTF-8 (the UTF-8 column). pcre2grep did not match it until I added -u, which then matched. And for \d, on this machine BSD grep accepted it as “digit” (echo 'abc 123' | /usr/bin/grep -oE '\d+' printed 123), but BSD sed -E did not: echo '12 d' | sed -E 's/\d/X/' printed 12 X, because it read \d as the letter d, and s/\w/X/ on ws 12 printed Xs 12. Check each tool before you rely on a shorthand there.
The POSIX spelling
For grep and sed, the portable names are POSIX classes written inside brackets: [[:digit:]], [[:alpha:]], [[:alnum:]], [[:space:]]. echo 'ab 12' | sed -E 's/[[:digit:]]+/N/; s/[[:alpha:]]+/W/' printed W N. They are not available in JavaScript, and worse, they don’t fail loudly. /[[:alpha:]]/ reads as the class of [, :, a, l, p, h followed by a ]:
/[[:alpha:]]/.test("a") // false
/[[:alpha:]]/.test("a]") // true
Matching a fails and matching a] succeeds. That is the sort of bug a one-example test can’t catch, which is why the testing lesson insists on negative cases.
About the video
The embed is a clip of Wendi Jollymore’s “Basic Regular Expressions 4 - Meta-Characters & OR Operator”, published on 18 April 2022 and 27 minutes 11 seconds long. It plays from 8:41 to 17:43, which her description labels “Meta-Characters (d, w)”, an exercise, and “Meta-Characters (dot, whitespace)”. That covers \d, \w and whitespace and also the dot, which has its own lesson. She uses regexr as the tester per the description. I couldn’t read a transcript, so I can’t say whether she mentions the Unicode and locale differences above, and I expect not. English per its title and description.
What I did not run
GNU grep, regex101, and Perl and PCRE with locale settings other than the ones in the table.
Next
Of the shorthands, one character is special enough to get its own lesson, because it is the shortcut for “anything”: The dot and newlines.
Check yourself
What does \w match in JavaScript?
Only ASCII letters, digits and the underscore, the same as [A-Za-z0-9_]. Accented letters, hyphens and spaces are not word characters.
Why does "café x_1".match(/\w+/g) return ['caf', 'x_1'] in JavaScript but ['café', 'x_1'] in Python 3?
JavaScript's \w is ASCII only, so é ends the match. Python's \w is Unicode-aware for str patterns and counts é as a letter. Python's re.ASCII flag gives the JavaScript result.
What happens when you write /[[:alpha:]]/ in JavaScript?
It reads as the class [[:alph] (the characters [ : a l p h) followed by a literal ]. It does not match a, but it does match the text ":]".
How do you match a run of word characters and hyphens such as foo-bar?
Use /[\w-]+/. The shorthand works inside the class, and the dash sits last so it is a literal.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.