Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 38 ยท Character Classes and Quantifiers

Shorthand Classes and Negation

Learn the regex shorthand classes \d, \w and \s and their negations \D, \W and \S. See exactly what they match in JavaScript, where they surprise you, and how to build safer alternatives.

Digits, "word" characters and whitespace are so common that regex gives them one-letter shortcuts. They are everywhere in real patterns, so it pays to know their exact meaning, including the places where they are narrower or wider than you expect.

The three shorthands

Shorthand Equivalent class Meaning
\d [0-9] an ASCII digit
\w [A-Za-z0-9_] a "word" character: letter, digit or underscore
\s [ \t\n\r\f\vย โ€จโ€ฉ๏ปฟ...] any whitespace or line terminator

Their uppercase versions are the negations: \D is any character that is not a digit, \W any non-word character, \S any non-whitespace character.

Node.js Shorthands in action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice that \w includes the underscore, which is why ship_it is one word, and that \S+ is a quick way to grab "whitespace-separated tokens".

What \s really matches

\s is wider than a space and a tab. In JavaScript it includes line breaks, the non-breaking space, and other Unicode space separators.

Node.js The whitespace family

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Zero-width spaces (U+200B) are not matched by \s, which sometimes explains "invisible" text that refuses to be trimmed.

Where \d and \w surprise you

\d only matches ASCII digits 0-9, never Arabic-Indic or other digits. \w matches no accented letters.

Node.js ASCII only

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

For international text, use Unicode property classes such as \p{L} (any letter) and \p{N} (any number). They are the subject of a later lesson in this chapter.

Negation traps

\W, \D and \S match one character that is not in the set; they are not "the opposite of the whole match". Two traps are common.

  1. \S versus "not matching". \S+ finds a run of non-space characters; it does not mean "a line with no spaces". For that, anchor: ^\S+$.
  2. Mixing negations to mean "anything". [\s\S], [\d\D] and [^] all match every character including newlines. They are a classic way to write "any character" without the s flag.
Node.js Anything, including newlines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Prefer the s flag today. [^] works only in JavaScript; other flavors treat it differently.

Shorthands inside classes

Shorthands work inside brackets and combine with other members: [\w-] is "word character or hyphen", [\d.]+ is "digits and dots", [^\s,]+ is "anything except whitespace and commas".

Node.js Shorthands in a class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Trimming and normalizing whitespace

\s is the workhorse of whitespace cleanup.

Node.js Whitespace cleanup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does \w match in JavaScript?

    \w is ASCII letters, digits and underscore only, even with the u flag.
  2. 2. Which pattern matches one character that is not whitespace?

    Uppercase shorthands negate their lowercase counterparts.
  3. 3. How do you match any character, including newlines, without the s flag?

    [\s\S] combines whitespace and non-whitespace, so every character is included.
  4. 4. Does \d match the Arabic-Indic digit ูฃ?

    In JavaScript \d is exactly [0-9]. Use \p{Nd} with the u flag for any decimal digit.

Summary

  • \d, \w, \s are shortcuts for digits, ASCII word characters and whitespace; \D, \W, \S negate them.
  • \s includes line breaks and many Unicode spaces but not the zero-width space.
  • \w and \d are ASCII only; use \p{L} and \p{N} for international text.
  • [\s\S] (or the s flag) matches every character; shorthands also work inside [...].

Next: Greedy Quantifiers