Regex to Collapse Blank Lines

All patterns
Replace6 chars · /g

Regex to Collapse Blank Lines

Helpful when normalizing pasted docs, generated markdown, or support transcripts with too much vertical space.

The expression

/\n{3,}/g

How to read it

A brace quantifier fixes how many times the previous token repeats.

gglobal — keep going after the first match instead of stopping

Find matches in your own text

no input
Try:

This expression is unanchored, so it finds every occurrence anywhere in the text. Highlighted runs are what a replace call would rewrite.

Examples, checked in your browser

4/4 verified

line1 line2

Matches.

a b

Matches.

line1 line2

No match — this is the kind of input the pattern rejects.

single line

No match — this is the kind of input the pattern rejects.

Use it in code

3 languages
JavaScript
const regex = /\n{3,}/g;
regex.test(input);
Python
import re
pattern = re.compile(r"\n{3,}")
bool(pattern.search(input))
Go
re := regexp.MustCompile("\\n{3,}")
matched := re.MatchString(input)

Other replace patterns

3
GroupReplace
Flags/g
Examples4
SafetyOK