Level 1: Foundations · Lesson 6 of 30
Shorthands \d \w \s
Shorthand classes are short names for common character sets: \d for digits, \w for word characters and \s for whitespace, plus their uppercase opposites.
Some character sets are used so often that regex gives them short names. \d means a digit, \w means a word character, and \s means a whitespace character. They save typing and make patterns easier to read. Like a bracket set, each shorthand matches exactly one character.
\d matches a digit. In JavaScript it is exactly [0-9]. Add a quantifier to grab whole numbers: \d+ turns Room 12 into the match 12. Be aware that in Python 3, .NET (C#) and Rust, \d matches Unicode digits by default, such as Arabic-Indic digits, not only 0 to 9. If you need strictly ASCII digits in those languages, write [0-9] instead.
\w matches a word character: a letter, a digit or an underscore. In JavaScript it equals [A-Za-z0-9_]. So \w+ matches hello, user_1 and abc123 as whole tokens, but stops at spaces, commas, hyphens and other punctuation. Note that a hyphen is not a word character, so well-known gives two matches.
\s matches whitespace: a space, a tab, a newline, a carriage return and a few other spacing characters. It is useful for matching gaps of uncertain size, such as \d+\s+\d+, which matches two numbers separated by one or more spaces or tabs.
Each shorthand has an uppercase opposite. \D matches any character that is not a digit, \W any character that is not a word character, and \S any character that is not whitespace. So \S+ grabs runs of visible characters, including punctuation, which is handy for splitting text into rough tokens.
Shorthands can be placed inside character sets. [\d.] matches a digit or a dot, and [\w-] matches a word character or a hyphen. This is a common trick for slugs and codes like user-name_01.
These six shorthands work in JavaScript, Python, PHP, Java, Go, C#, Rust and Ruby. Remember to escape the backslash when you write a pattern inside a normal string: in JavaScript and Java the string "\\d+" holds the pattern \d+. Python raw strings like r"\d+" avoid the double backslash.
Live tester
4 matches (JavaScript engine)
Call 555 0100 at 9 pm, room 12B.
Examples
Numbers
/\d\d\d/gCodes 123 45 6789
Matches: “123”, “678”
Three digit shorthands in a row match 123 and the first three digits of 6789.
Text
/\w+/ghello_world, foo bar!
Matches: “hello_world”, “foo”, “bar”
Word characters include the underscore but stop at commas, spaces and the exclamation mark.
Codes
/ID\d+/gID7 ID42 IDX
Matches: “ID7”, “ID42”
The digit shorthand requires at least one digit after ID, so IDX is skipped.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match every whole number in the sentence.
Room 12, floor 3, code 450
Task 2: Match each word, including underscores, without the trailing punctuation.
hi there, user_1!
Task 3: Match two numbers separated by whitespace, but not when they are joined or separated by a hyphen.
time 10 30, 1030, 10-30