RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex to match empty lines

/^[ \t]*$/gm

The m flag makes ^ and $ match at every line boundary rather than only at the ends of the string — without it this tests whether the entire input is blank. Allowing spaces and tabs catches lines that look empty but are not.

What each part does

^start of a line, because of the m flag
[ \t]*any number of spaces or tabs, including none
$end of that line
mmultiline: ^ and $ apply per line

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
(empty string)matches(empty string)
matches
\tmatches\t
xno match
x no match

The mistake to avoid

Omitting the m flag and concluding the pattern is broken. Without m there is exactly one ^ and one $ in the whole document.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers