Regexdrill

Patterns for common data Updated November 2026

Regex for whitespace

\s matches any whitespace character: space, tab, newline, carriage return, form feed and Unicode spaces such as the non-breaking space. Use \s+ for runs, ^\s+|\s+$ to trim, and \S for non-whitespace. To collapse repeated spaces, replace \s+ with a single space.

Syntax

\s  (one whitespace character)   \S  (one non-whitespace character)

Examples

"  hello   world  ".replace(/^\s+|\s+$/g, "")

Result: "hello world"

Trim both ends; the same as .trim(). Inner spaces are untouched.

"a   b \t c\nd".replace(/\s+/g, " ")

Result: "a b c d"

Collapse any run of whitespace, including tabs and newlines, to one space.

"one two  three".split(/\s+/)

Result: ["one", "two", "three"]

["no_spaces", "has space", "   "].map(s => /^\s*$/.test(s))

Result: [false, false, true]

^\s*$ detects empty or blank-only strings.

Common mistakes

  • Using a literal space instead of \s, which misses tabs and newlines.
  • Calling replace(/\s/, "") without the g flag, which removes only the first whitespace character.
  • Splitting on \s+ without trimming first: " a b".split(/\s+/) returns an empty first element.
  • Assuming \s is ASCII-only: in JavaScript it also matches the non-breaking space (U+00A0) and other Unicode spaces, which is usually what you want.

Practice Regex for whitespace

These drills check your answer on live data, free, no account needed.

  • Trim whitespaceReplace to remove all whitespace at the start and end of each line, leaving inner spacing alone. Every occurrence must be replaced, so think about the g flag.
  • Collapse repeated spacesReplace every run of two or more spaces with a single space.
  • Character class: gray or greyThe whole line must match. Match both spellings, gray and grey, and nothing else.

Questions

How do I match a tab in regex?

\t matches a tab. [ \t] matches a space or tab but not newlines, which is useful when you want to stay on one line.

What is the regex for an empty or whitespace-only string?

^\s*$ matches an empty string or one made only of whitespace. Use \S to test that a string has at least one visible character.

Does \s match a newline?

Yes, \s includes \n and \r. To match horizontal whitespace only, use [ \t] or, in engines that support it, \h.

Related

  • Regex for numbers onlyThe ^\d+$ pattern for digits-only strings, stripping everything that is not a digit, extracting numbers from text, and patterns for signed and decimal values.
  • Character classesWhat a character class matches, how ranges and negation work, what \d \w \s and their uppercase opposites mean, and what the dot does and does not match.
  • Dot and newlineWhy . does not match line breaks by default, the s flag in JavaScript and re.DOTALL in Python, the portable [\s\S] trick, and how s differs from m.
  • Escaping special charactersThe regex metacharacters that must be escaped, how to match a literal dot, dollar, plus or slash, what changes inside brackets, and how to escape user input.

Previous: Regex for UUIDNext: Regex for numbers only