Skip to content
Free Tool Arena

How-To & Life · Guide · Text & Writing Utilities

How to Remove Line Breaks

LF vs CRLF vs CR, paragraph preservation, bulk flatten, undo-able strategies, and regex patterns.

Updated April 2026 · 6 min read

Line breaks are the most environment-dependent characters in plain text. Windows uses CRLF, Unix uses LF, classic Mac used bare CR, and PDFs, emails, and scraped web pages mix all three with abandon. Removing line breaks sounds like a one-liner but actually requires you to decide what you’re preserving: paragraphs? Sentences? Bulleted structure? A blanket strip destroys structure; a naive regex misses one of the three line-ending variants. This guide covers the three line-ending types, the patterns that work across all of them, paragraph-preserving strategies, and the pitfalls that show up when you re-import the cleaned text into another tool.

Advertisement

The three line endings

Only three characters matter, and their combinations are the source of most pain:

  • LF (\n, U+000A) — Unix, macOS, Linux
  • CRLF (\r\n, U+000D U+000A) — Windows, most email, HTTP
  • CR (\r, U+000D) — classic Mac, rare now but still in some exports

A file scraped from a Windows-origin email and saved through a Mac text editor can contain all three.

Why the naive approach fails

text.replace(/\n/g, ” ”) only hits LF. On a CRLF file this leaves an orphan CR behind every line, which displays as a strange box or causes cursor-carriage behavior in some editors.

input (CRLF):  "line 1\r\nline 2\r\n"
after /\n/g:    "line 1\r line 2\r "
                        ^        ^ orphan CRs

The universal line-break regex

Match all three variants in any order:

/\r\n|\r|\n/g

This matches CRLF as a unit (so you don’t double-replace), then bare CR, then bare LF. Order matters — put CRLF first.

Flatten to single line

Replace every break with a space, then collapse runs of spaces:

text
  .replace(/\r\n|\r|\n/g, " ")
  .replace(/\s+/g, " ")
  .trim()

Use this for copy-pasting text from a PDF into a word processor when each visual line is a soft line-break and you want running prose.

Preserve paragraphs, flatten within

Most copy-from-PDF cases want paragraphs preserved but single wraps flattened. Detect paragraph breaks (two or more line breaks in a row), replace single breaks with a space, then restore paragraphs.

text
  .replace(/\r\n?/g, "\n")          // normalize to LF
  .replace(/\n{2,}/g, "\u0000")     // temp marker for paragraphs
  .replace(/\n/g, " ")              // flatten single breaks
  .replace(/\u0000/g, "\n\n")      // restore paragraphs

Normalize first, then operate

The cleanest strategy: always normalize to LF first. Makes every downstream rule simpler.

const lf = text.replace(/\r\n?/g, "\n");

After normalization, \n is the only line break you ever have to match.

Preserving bulleted and numbered lists

Bulleted list items look like “- item” or “1. item” at the start of a line. Flattening them destroys the list. Detect them before flattening:

// Don't flatten breaks that precede bullet patterns
text.replace(/\n(?!\s*(?:[-*•]|\d+\.))/g, " ")

This keeps breaks before bullet lines and flattens everywhere else. Adjust the character class for your bullet style.

Handling soft-wrap from PDFs

PDFs frequently break mid-word with a hyphen. Remove the hyphen and the break to re-flow:

text.replace(/-\n/g, "")   // de-hyphenate
    .replace(/\n/g, " ")

Watch out for genuine hyphenated compounds (“re-\nfactor” becomes “refactor” when you wanted “re-factor”). Hard to fix without a dictionary; usually acceptable.

Round-tripping: be reversible

If you need to undo the cleanup, keep a copy of the original. Line break removal is not reliably reversible — once you collapse “end of sentence.\nNext sentence” to “end of sentence. Next sentence,” you can’t recover the original break. Version your text at each step.

Bulk flatten across many files

For batch jobs, normalize and strip in a loop:

# bash one-liner
for f in *.txt; do
  tr -d '\r' < "$f" | tr '\n' ' ' > "${f%.txt}.flat"
done

Or in Python with pathlib + io. Always write to new filenames first, diff, then replace.

When to keep line breaks

Some content genuinely needs breaks preserved: code, poetry, addresses, CSV-style data, chat transcripts. If the text has any structural meaning encoded in line layout, think twice before stripping. A flatten pass is destructive.

Common mistakes

Replacing only \n and leaving orphan \rcharacters behind. Flattening paragraphs and losing all structure. Removing soft-wrap hyphens without also handling real compound words. Forgetting to normalize first, then writing three separate regexes for each line-ending variant. And operating on the original file with no backup.

Run the numbers

Line break removerWhitespace removerRemove duplicate lines

Advertisement

Found this useful?Email