PatternRegex

Find blank lines

Locate empty or whitespace-only lines in a file or document, useful for tidying up extra spacing before a diff or a print.

Last updated

Patterngm
^\s*$
Open in Regex Tester & Builder

Matches

  • Line one Line three
  • Para one Para two

Doesn’t match

  • Line one Line two Line three
  • No blank lines All rows have text Final row here

Why it’s written this way

\s*$ says a line may contain zero or more whitespace characters and nothing else, so it matches both a truly empty line and one that's only spaces or a leftover tab.

The m flag redefines ^ and $ to mean the start and end of each line rather than the start and end of the whole string, which is what lets one pattern check every line of a multi-line document in a single pass.

Edge cases to know

  • →\s also matches newline characters, so a run of several blank lines back to back can produce more matches than you'd expect from counting them by eye.
  • →It treats a line of pure spaces exactly like a truly empty line — if you need to tell those apart, swap \s*$ for a check that excludes newline explicitly.

Part by part

^
Start of a line, because the m flag is set. Without it this would only ever test the first line of the string.
\s*
Zero or more whitespace characters, which is what makes a line of spaces count as blank alongside a genuinely empty one.
$
End of the same line. Together with the start anchor it asserts the line contains nothing but whitespace.

Common mistakes

  • ^$

    Only strictly empty lines match, so a line containing a single space or a tab is skipped — and those are exactly the lines that look blank but are not, which is usually why you are searching.

    ^\s*$
  • ^\s*$ without the m flag

    Without m the anchors apply to the whole string, so the pattern matches only if the entire input is whitespace. On a multi-line document it silently finds nothing.

    ^\s*$ with flags gm
  • \n\s*\n

    Matching the surrounding newlines instead consumes them, so replacing consecutive blank lines leaves the paragraphs joined. It also cannot match a blank line at the very start or end of the text.

Questions

How do I collapse several blank lines into one?

Match the runs rather than the lines: replace two or more consecutive newlines with two. Using this pattern with a global replace removes the content of blank lines but not the lines themselves, which is rarely what people expect.

Does a line of tabs count as blank?

Yes. The whitespace class covers tabs, spaces and other space characters, so any line that renders as empty is matched — which is the useful definition when you are cleaning a document.

Why does it match at the end of the file?

Because a trailing newline creates a final empty line after it. Most tools consider a file ending in exactly one newline correct, so a single match there is usually expected rather than a problem.

Does it work on Windows line endings?

Yes. The carriage return is itself a whitespace character, so it is absorbed by the star and a CRLF blank line matches the same as a Unix one.

Related in Patterns