How to Clean Up Pasted Text With Find and Replace
By Khushi Yadav
To clean up pasted text, paste it into an editor with find and replace, fix it there, then copy the result. For anything beyond a single fix, use regular expressions in a code editor such as VS Code: one pattern can join every broken line from a PDF, strip tracking codes from a list of links or remove timestamps from a chat log, and you can preview every match before replacing. Word's Find and Replace handles simpler jobs such as stray line breaks and non-breaking spaces.
Set up VS Code for find and replace
- Open a new file (Ctrl+N) and paste the text.
- Press Ctrl+H to open Replace.
- Turn on regular expressions with the .\* button in the Find box (or Alt+R).
- Type the pattern in the first box and the replacement in the second. Matches are highlighted as you type, so check them before replacing.
- Select Replace All (Ctrl+Alt+Enter), then copy the cleaned text.
VS Code's find box uses JavaScript regular expressions. In the replacement, $1 and $2 insert the text matched by the first and second groups in parentheses. The patterns below are written for it.
Patterns for common cleanup jobs
| Job | Find | Replace with |
|---|---|---|
| Join lines broken inside paragraphs (paragraphs separated by blank lines) | (?<!\n)\n(?!\n) | a single space |
| Join a line to the next when the next starts lowercase | ([a-z,;])\n([a-z]) | $1 $2 |
| Rejoin words hyphenated at line ends | (\w)-\n(\w) | $1$2 |
| Remove trailing spaces and tabs | [ \t]+$ | nothing |
| Collapse three or more line breaks into one blank line | \n{3,} | \n\n |
Remove chat timestamps like [12:04] or [9:05:33] | ^\[\d{1,2}:\d{2}(?::\d{2})?\]\s* | nothing |
| Replace non-breaking spaces with normal spaces | \u00A0 | a single space |
| Remove soft hyphens and zero-width spaces | [\u00AD\u200B] | nothing |
| Straighten curly double quotes | [\u201C\u201D] | " |
| Straighten curly single quotes and apostrophes | [\u2018\u2019] | ' |
A few notes on using them:
- Broken PDF lines: if paragraphs in your text are separated by a blank line, the first pattern joins every other line break and keeps the paragraphs. If they aren't, use the second, more cautious pattern, which only joins a line to the next when the next one starts with a lowercase letter. How to copy from a PDF without broken line breaks covers getting cleaner text in the first place.
- Hyphenated line ends: check the matches first. Words that are always hyphenated, such as "well-known", get joined too if they happen to break at the hyphen.
- Timestamps: remove names and reactions by hand; a pattern can't know which parts of a conversation you want to quote. How to quote a chat log without leaking extras covers what else to remove.
Remove tracking codes from a list of links
With one URL per line, run this replacement:
- Find:
([?&])(utm_[a-z]+|fbclid|gclid)=[^&\s#]*&? - Replace with:
$1
Select Replace All twice, because two tracking codes in a row need two passes. Then remove the ? or & left dangling at the end of a link:
- Find:
[?&]+(?=\s|$|#) - Replace with: nothing
https://example.com/page?utm_source=x&id=7 becomes https://example.com/page?id=7. Check that the links still open. For one link at a time, how to copy a clean link without tracking parameters is quicker.
Clean text in Word
Word's Find and Replace doesn't use regular expressions, but it can find the characters that usually cause trouble:
- Press Ctrl+H, then select More.
- Click in Find what, open the Special menu and pick the character: Manual Line Break, Paragraph Mark, Nonbreaking Space or Optional Hyphen.
- Enter the replacement (a space, a paragraph mark, or nothing) and select Replace All.
Turning manual line breaks (^l) into paragraph marks (^p), or the reverse, fixes most pasted text with odd spacing. Word also has its own wildcard syntax under Use wildcards, but it isn't regular expressions; patterns from this page won't work there.
Clean text in Excel
For pasted columns, use formulas in a helper column, then copy and Paste Special > Values over the original:
=TRIM(A1)removes extra spaces, but not non-breaking spaces.=SUBSTITUTE(A1,CHAR(160)," ")turns non-breaking spaces into normal spaces.=CLEAN(A1)removes non-printing control characters such as line breaks inside a cell.
Ctrl+H in Excel replaces plain text across a selection, such as removing a currency symbol. For numbers specifically, see how to copy a number without the currency symbol.
Clean the clipboard directly with PowerShell
If you repeat the same cleanup often, PowerShell can read the clipboard, fix it and put it back. This joins lines broken inside paragraphs:
(Get-Clipboard -Raw) -replace '\r\n', "`n" -replace '(?<!\n)\n(?!\n)', ' ' | Set-Clipboard
And this replaces non-breaking spaces:
(Get-Clipboard -Raw) -replace '\u00A0', ' ' | Set-Clipboard
Copied text on Windows usually has \r\n line endings, so the first command converts them to \n before joining lines. Save one-liners you use often in a script so the cleanup is the same every time; the PowerShell clipboard cheat sheet has more.
When a clipboard tool is enough
For a single paste, you don't need an editor:
- Plain text: Ctrl+Shift+V pastes without formatting in many apps, and PowerToys Advanced Paste pastes as plain text anywhere with Win+Ctrl+Alt+V. How to paste without formatting on Windows covers the options.
- Quick fixes: Ditto's Special Paste menu includes Trim White Space, Remove Line Feeds and case changes for a single clip.
These act on one clip at a time with no preview. For a long text or a list, the editor is faster and safer.
| Job | Best tool |
|---|---|
| Drop fonts and colours from one paste | Paste as plain text |
| Change case of a heading | Word's Shift+F3 or your editor |
| Clean a list of links or a long text | VS Code with regex |
| Fix a pasted column | Excel formulas, then Paste Special > Values |
| Reuse the same sentence every day | A text expander |
| Repeat the same cleanup every day | A saved PowerShell script, or a Power Automate for desktop flow with clipboard actions |
FAQ
How do I remove line breaks from copied text?
Paste it into VS Code, open Replace (Ctrl+H), turn on regex and replace (?<!\n)\n(?!\n) with a space. That joins lines within paragraphs and keeps blank lines between them.
Why didn't TRIM remove the spaces in Excel?
They're probably non-breaking spaces from a web page. Use =SUBSTITUTE(A1,CHAR(160)," ") first, then TRIM.
Related reading
- How to Copy Text From a PDF Without Line Breaks
- How to Remove Hidden Characters From Copied Text
- How to Change Case (UPPER/lower/Title) on Pasted Text
- PowerShell Clipboard Cheat Sheet: Get-Clipboard and Set-Clipboard
Sources
- Basic editing: Find and Replace (VS Code docs) — regular expressions,
$ncapture groups in replacements. - Find and replace text in Word (Microsoft Support) — the Special menu and wildcards.
- Get-Clipboard (Microsoft Learn) — the
-Rawparameter. - Set-Clipboard (Microsoft Learn) — writing text back to the clipboard.
- Advanced Paste (Microsoft Learn) — Paste as plain text and its shortcut.
- Ditto (GitHub) — Special Paste options.
Khushi Yadav writes Edge Drop's workflow and design guides and is a B.Tech Computer Science Engineering student. Khushi's posts are checked against Microsoft's documentation and the Edge Drop fact sheet.
GitHub · LinkedInCopy. Stack. Drop.
Transform your clipboard into an interactive edge shelf. Stack, pin, and drag assets into any app with zero friction.
Download for Windows Get from Microsoft Store
How to Install Guide · First 10 Minutes Guide · Drag & Drop Guide · Edge Drop vs Win+V · Support
Free · Lightweight · Privacy First