Regex for Matching a Whitespace-Only String

Matches a string that contains only whitespace characters (or is empty).

Published October 10, 2026

Matches a string that contains only whitespace characters (or is empty).

Key points

  • Useful for detecting a field that was "filled in" with only spaces or tabs
  • \s matches spaces, tabs, and newlines

Example

^\s*$
More General articles