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 |
| m | multiline: ^ 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.
| Input | Result | Matched |
|---|---|---|
| (empty string) | matches | (empty string) |
| matches | ||
| \t | matches | \t |
| x | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID