DEV Community

Sam Grg
Sam Grg

Posted on

How to Remove Line Breaks and Markdown When Copying from ChatGPT (and PDFs)

If you regularly copy text from ChatGPT, PDFs, or web pages into Google Docs, Microsoft Word, Notion, or emails, you have probably run into the same frustrating problem: broken paragraphs, random hard line breaks, and leftover Markdown (**bold**, ### headings, backticks).

Here is why it happens and the fastest ways to fix it — whether you prefer keyboard shortcuts, JavaScript regex, or a 1-click browser tool.


Why Copy-Pasted Text Breaks

  1. PDFs store a hard line break at the end of every printed line rather than every sentence. Copying a paragraph brings every visual line break along.
  2. ChatGPT & AI tools output raw Markdown with double/triple newlines, ** bold markers, and ### headings. Pasting into Word or Docs often leaves raw symbols or extra gaps behind.

Method 1: Quick Regex in JavaScript

If you are building a web app or cleaning text in the browser console, you can normalize line breaks and collapse extra whitespace in two lines of JavaScript:

const cleanText = rawText
  .replace(/(\r\n|\r|\n)+/g, ' ')
  .replace(/\s{2,}/g, ' ')
  .trim();
Enter fullscreen mode Exit fullscreen mode

If you want to preserve paragraph breaks while collapsing triple newlines:

const cleanParagraphs = rawText
  .replace(/\r\n/g, '\n')
  .replace(/\n{3,}/g, '\n\n')
  .trim();
Enter fullscreen mode Exit fullscreen mode

Method 2: Built-In App Shortcuts

  • Microsoft Word: Press Ctrl + H, search for ^p (paragraph break) or ^l (manual line break), and replace with a single space.
  • Google Docs: Press Ctrl + H, check Use regular expressions, search for \n, and replace with a space.
  • Excel: Use =CLEAN(SUBSTITUTE(A1, CHAR(10), " ")) to strip newline characters inside cells.

Method 3: 1-Click Free Online Cleaners (No Sign-Up)

If you don't want to run regex or Find & Replace every time, I built a fast, 100% client-side set of text utilities that run locally in your browser (nothing is uploaded or stored):

  • Line Break Remover — Paste text from PDFs, emails, or docs and replace line breaks with spaces, commas, or custom delimiters in one click.
  • ChatGPT Text Formatter — Strips extra line breaks, removes Markdown (**, *, ###, `), and cleans bullet prefixes before pasting into Word, Docs, or email.

Hope this saves you a few minutes of manual cleanup! Let me know what other text-cleaning edge cases trip you up.

Top comments (0)