Whitespace-Only Line
Matches lines containing only whitespace (or empty lines).
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("^\\s*$", "gm");
const input = "line one\n \n\nline two";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"^\s*$", re.MULTILINE)
input_text = "line one\n \n\nline two"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?m)^\s*$`)
input := `line one
line two`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
^\s*$ (flags: gm)Raw source: ^\s*$
How it works
Examples
Input
line one
line twoMatches
Common use cases
- •Whitespace cleanup
- •Linters
- •Blank-line detection
Related patterns
Trailing Whitespace (Per Line)
Text ProcessingMatch trailing spaces and tabs at the end of each line — the regex linters use to flag dirty whitespace.
Whitespace Trim (Leading & Trailing)
Text ProcessingMatch leading and/or trailing whitespace on a string — the regex equivalent of .trim().
Single-Line Comment (// or #)
Text ProcessingMatch single-line comments using either the `//` (C-family) or `#` (shell, Python, Ruby, YAML) marker.
C-Style Block Comment
Text ProcessingMatch C-style /* ... */ block comments across multiple lines.
Related concepts
How to Match Whitespace in Regex
How-to\s matches any whitespace — space, tab, newline, and more. Use \s+ to collapse runs, ^\s*$ to detect blank lines, and \S for non-whitespace.
How to Replace Text Using Regex in JavaScript
How-toString.replace and String.replaceAll accept a regex. Use the g flag for multi-match replace, $1/$2 for capture references, and a function for complex logic.