How-To & Life · Guide · Text & Writing Utilities
How to Remove Line Breaks
Flatten text line breaks while keeping paragraph structure intact free online. Use regex patterns and undo-able strategies instantly with no download required.
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 CRsThe 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 paragraphsNormalize 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"
doneOr 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
Use these while you read
Tools that pair with this guide
- Line Break RemoverPaste text with line breaks and get a single paragraph, space-joined, or custom-separator output. Useful for emails and forms.Text & Writing Utilities
- Whitespace RemoverClean pasted text by collapsing repeated spaces, tabs, and newlines. Trim edges, normalize to single spaces, or strip entirely.Text & Writing Utilities
- Remove Duplicate LinesPaste text and remove duplicate lines instantly with one click. Adjust case sensitivity and whitespace handling right in your browser, completely free.Text & Writing Utilities
- Word CounterFree word counter. Paste text and see words, characters, sentences, and reading time instantly. Works offline after load.Text & Writing Utilities
Advertisement
Continue reading
- How-To & LifeHow to Write Numbers in WordsConvert any number into words for checks or legal documents instantly. This free online tool handles cardinal and ordinal formats, with no registration or sign-up needed.
- How-To & LifeHow to Count Word FrequencyCount word frequency online instantly with our free text analyzer. Remove stop-words and explore n-grams for SEO research without registration.
- How-To & LifeHow to Remove Duplicate LinesDedup text lines using exact, trimmed, or case-insensitive comparison free online. Process large files and choose preserve-first or preserve-last instantly with no signup.
- How-To & LifeHow to Detect Invisible CharactersFind zero-width joiners, non-breakers, and byte order marks that break your regex after paste. Use this free instant guide for clean text processing online.
- How-To & LifeHow to Normalize Unicode TextNormalize Unicode text using NFC, NFD, NFKC, and NFKD forms for search and security. A free online guide to handling homoglyph attacks and key normalization in seconds.
- How-To & LifeHow to Strip Special CharactersStrip special characters from text: define what 'special' means, clean to ASCII‑only, preserve spaces and punctuation, and produce URL‑safe output. Free guide, no sign‑up.