Remove Blank Lines in Text With Regex (CRLF and LF Guide)

PublishedToolBlank Line Remover

Contents
  1. Why blank lines keep piling up
  2. Deleting them yourself with a regex
  3. Line break characters on Windows vs. Mac and Linux
  4. Common snags
  5. The short version
  6. Frequently asked questions

When you scrape a table off a web page and paste it, or copy logs an outside system spits out, you often end up with a bunch of stray blank lines wedged between paragraphs. The document gets longer and harder to read. Instead of backspacing line by line, here's how to target only the blank lines with a regular expression, or collapse runs of empty lines into one.

Summary ① To delete only blank lines, a single regex does it: /^\s*\n/gm ② If several lines in a row are empty, you can merge them into one and keep your paragraph breaks ③ Windows (CRLF) and Mac/Linux (LF) use different line break characters, so your regex needs \r?\n to catch both

Why blank lines keep piling up

Copy a table from a web page and each cell often comes across separated by a line break, dragging blank lines along with it. Open server logs or a CSV in a text editor and you'll find invisible trailing spaces and empty rows mixed in too.

Once a document runs past a few hundred lines, picking out blank lines by hand isn't realistic. Move down with the arrow keys and delete one line at a time, and it's easy to wipe a line that actually had content.

Deleting them yourself with a regex

You don't need a tool for this. Your text editor's Find and Replace (with regex enabled) can produce the same result.

To delete only fully blank lines: the pattern for a line with nothing but whitespace, or nothing at all, is /^\s*\n/gm. ^ is the start of a line, \s* means zero or more whitespace characters, and \n is the line break. You need both the g (global) and m (multiline) flags so it applies to every line in the document. Replace matches with an empty string.

To merge consecutive empty lines into one: if you want to keep your paragraph breaks, find /\n{3,}/g (three or more line breaks in a row) and replace it with \n\n (two line breaks, meaning one blank line). This keeps the spacing between paragraphs while shrinking any vertical gap that stretched to three lines or more.

Check it with our tool. If typing regexes is a hassle, just paste your text in and the same result appears right away.

Blank Line Remover

Paste your text and the blank lines get cleaned up right away. Choose between "Remove all blank lines" and "Merge consecutive blank lines", then copy the result.

Line break characters on Windows vs. Mac and Linux

The thing that's easy to miss when handling blank lines with regex is the line break character per operating system.

Operating systemLine break characterSize
WindowsCR+LF (\r\n)2 bytes
macOS / LinuxLF (\n)1 byte

When you process a file created on Windows with a regex, searching only for \n can leave a \r behind at the end of each line. So write the pattern as \r?\n — CR may or may not be there — to catch both styles.

Common snags

SituationWhat to check
Lines with only spaces or tabsThey count as blank lines, so they get removed or merged too
Trailing whitespace at the end of linesSeparate from the blank-line question — turn on the option that trims whitespace at the start and end of each line and it gets cleaned up as well
Invisible special spaces (zero-width space, etc.)They may not register the same way as ordinary spaces, tabs, and line breaks, so it's safest to eyeball the result once after processing
You want to keep paragraph breaksDon't delete every blank line — choose the option that merges consecutive blank lines into one

The short version

  • To delete only fully blank lines, replace /^\s*\n/gm with an empty string.
  • To keep paragraph breaks, merge consecutive blank lines into a single line.
  • Windows text uses \r\n and Mac/Linux uses \n, so use a pattern like \r?\n that catches both.
  • If typing regexes is a hassle, paste your text in and you can see the same result right away.
Blank Line Remover

Paste your text and the blank lines get cleaned up right away. Choose between "Remove all blank lines" and "Merge consecutive blank lines", then copy the result.

Frequently asked questions

What's the difference between "delete all blank lines" and "merge consecutive blank lines"?

Delete all blank lines removes every blank line with none left behind. Merge consecutive blank lines reduces multiple blank lines to one so your paragraph breaks survive.

Are lines with only spaces or tabs treated as blank lines?

Yes. A line with no visible characters, only spaces or tabs, is still recognized as a blank line, so it's subject to removal or merging.

Is the text I paste stored on a server?

No. Processing runs entirely in your browser, and what you enter is never sent to a server.

This article was adapted from our Korean original. Read the Korean original

Related articles

  1. Remove Forced Line Breaks from PDF Text and Merge ParagraphsLearn how to strip forced line breaks from PDF and email text, merge split lines back into clean paragraphs, and fix duplicate spaces.
  2. Markdown Comment Removal: Regex Formula and Build-Time StrippingSee how a non-greedy regex strips HTML comments from Markdown, catch the edge cases, and run the same cleanup during your static build.
  3. Resume action verbs: build result-focused bullet pointsHow to write resume bullets that lead with an action verb and end with a metric, plus verb lists for leadership, technical, research, and process work.
  4. SameSite cookie setup: 6 attributes and how each one worksSee how Domain, Path, Max-Age, SameSite, Secure and HttpOnly shape when and where cookies are sent, and why document.cookie and Set-Cookie differ.
  5. async vs defer: how script loading and execution differSee how default, async, and defer scripts change HTML parsing, download, and execution on a timeline, and why parser blocking affects Core Web Vitals.
  6. CSS scrollbar color: why it works in Chrome but not FirefoxWhy scrollbar color CSS works in Chrome but not Firefox: the ::-webkit-scrollbar vs scrollbar-color difference, plus how to generate both codes at once.

Related tools