Regex to Match Whitespace-Only Strings

Detect strings containing only whitespace characters including spaces, tabs, newlines, and other Unicode whitespace. Useful for input validation. Free regex tester.

Regular Expression

/^\s+$/

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
\sMatches any whitespace character (space, tab, newline)
+Matches the preceding element one or more times (greedy)
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex detects strings that consist entirely of whitespace characters with no visible content. Here is the token-by-token breakdown:

^ — Anchors the match at the start of the string, ensuring the check begins from the very first character.

\s+ — Matches one or more whitespace characters. The \s shorthand character class matches a variety of whitespace characters including: regular space (U+0020), tab (U+0009), newline or line feed (U+000A), carriage return (U+000D), vertical tab (U+000B), and form feed (U+000C). In Unicode-aware mode, it also matches non-breaking spaces and other Unicode whitespace characters.

$ — Anchors the match at the end of the string, ensuring nothing follows the whitespace.

No flags are used since this validates a single string for being entirely whitespace.

This pattern is one of the most commonly used validation patterns in web development. It detects strings that look empty to users but technically contain characters. This is important for form validation where empty-looking input should be rejected, trimming user input before processing, content moderation to detect blank submissions, and database hygiene to prevent storing whitespace-only values.

The pattern requires at least one character (\s+ not \s*), so truly empty strings (zero length) will not match. If you need to also match empty strings, use \s* instead. This is a deliberate design choice since empty strings and whitespace-only strings often need different handling.

This pattern is useful in contact forms, search inputs, comment systems, and any user-facing input field where blank submissions should be prevented.

Example Test Strings

InputExpected
Match
Match
hello No Match
Match
visibleNo Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 5 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →