How to Clean Up Messy Pasted Text from Word and PDFs

To clean up messy pasted text, work in a fixed order: strip any leftover markup, delete zero-width characters, turn non-breaking spaces and tabs into ordinary spaces, rejoin the lines a PDF broke, and straighten quotes only if the text is going into code or data. "Paste as plain text" won't do this for you. It removes formatting, but a non-breaking space or a curly quote isn't formatting. It's a character, and it comes along for the ride.
Below is what each troublemaker is, why it causes problems, and the exact fix. The patterns are JavaScript-flavoured regular expressions, which is what our Find & Replace and Regex Tester use.
Why paste as plain text isn't enough
Most editors have a paste-without-formatting command. In Google Docs it's Ctrl+Shift+V, or ⌘+Shift+V on a Mac, according to Google's Docs keyboard shortcut list. Use it, because it drops fonts, colours, links and other styling.
But plain text is still Unicode, and Unicode has several characters that look exactly like a space, a few that look like nothing at all, and two pairs of quotation marks that look almost like the ones on your keyboard. They survive any plain-text paste. You only notice them later, when a search finds nothing, a spreadsheet says two identical names are different, or a pasted command fails for no visible reason.
Find out what's hiding in the text
You can't fix what you can't see, so start with two quick checks:
- Compare characters with bytes. Paste the text into our character counter, which shows characters and UTF-8 bytes side by side. In plain English with straight quotes the two numbers match, because every ASCII character is one byte. A non-breaking space takes two bytes; a curly quote or a zero-width space takes three. So a gap between the numbers means something non-ASCII is in there. Accented letters and emoji widen the gap too, so treat it as a hint, not a verdict.
- Highlight every non-ASCII character. In the regex tester, enter the pattern
[^\x00-\x7F]with thegflag. Every match is highlighted, and the match details list gives each one's position, so the invisible character at index 1,204 stops being a mystery.
The usual suspects in pasted text
| Character | Code point | Looks like | Where it usually comes from |
|---|---|---|---|
| No-break space | U+00A0 | A normal space | Word processors, web pages ( ), PDFs |
| Narrow no-break space | U+202F | A thin space | French-formatted numbers such as 1 234 567 |
| Zero-width space | U+200B | Nothing | Web pages, where it marks an optional line break |
| Byte order mark | U+FEFF | Nothing | The start of some text files |
| Soft hyphen | U+00AD | Nothing, or a hyphen at a line end | Web pages, where it marks a place a word may break (­) |
| Curly quotes | U+2018, U+2019, U+201C, U+201D | ‘ ’ “ ” | Smart quotes in word processors |
| Tab | U+0009 | A wide gap | Tables, columns, indented lists |
Each of these is legitimate somewhere. The problem is when they land somewhere that expects plain spaces and straight quotes: a database, a spreadsheet lookup, a URL, a line of code.
How to fix spaces, invisible characters and quotes
No-break spaces, tabs and doubled spaces
The quickest fix is the Clean whitespace button in our case converter. On every line it turns any run of whitespace (spaces, tabs, no-break spaces and the other Unicode spaces JavaScript recognises) into a single ordinary space, trims both ends of the line, and deletes empty lines. That's ideal for lists, names and form fields.
The catch is that last step: the blank lines between paragraphs disappear too. If you need to keep them, use Find & Replace with the Regex box ticked instead:
Collapse spaces, tabs and no-break spaces:
Find: [ \t\u00A0\u202F]+
Replace: (a single space)
Remove trailing spaces at the end of each line:
Find: [ \t]+(?=\n|$)
Replace: (leave empty)The lookahead in the second pattern matters. Our Find & Replace runs every pattern with the g flag (plus i if you tick Case insensitive) but not the multiline m flag, so a bare $ only matches the very end of the text, not the end of each line.
If the tabs are column separators, as in data copied from a spreadsheet, don't collapse them. You'd be destroying the structure. Convert the data properly instead, for example from TSV to CSV.
Zero-width characters and soft hyphens
Clean whitespace won't catch a zero-width space, because JavaScript doesn't count it as whitespace. Remove these explicitly:
Find: [\u200B\u2060\uFEFF\u00AD]
Replace: (leave empty)That covers the zero-width space, the word joiner, the byte order mark and the soft hyphen. Notice what's missing: the zero-width joiner (U+200D) and non-joiner (U+200C). Leave those alone unless you know the text is plain English. The joiner glues emoji sequences together (👩💻 is a woman, a joiner and a laptop), and the non-joiner is part of correct spelling in Persian and some other scripts.
Smart quotes
We'd keep curly quotes in anything people will read, because they're typographically correct. Straighten them when the text is headed for code, JSON, CSV, a terminal or a config file, where only the straight characters count as quotes:
Find: [“”] Replace: "
Find: [‘’] Replace: 'Run the second one with care in prose: it also turns every apostrophe in "don’t" into a straight one, which is usually what you want for code and rarely what a designer wants.
Fixing line breaks in text copied from a PDF
A PDF records where each line of text sits on the page, not where paragraphs begin and end. Copy from one and you usually get a hard line break at the end of every line, plus a hyphen wherever the layout split a word.
For a single paragraph, the Join lines button (in the converter's Text operations tab) does the job: it joins all lines with spaces and collapses any doubled spaces. Run it on several paragraphs, though, and you'll get one giant paragraph, because it joins everything.
To keep paragraphs, use two regex passes in Find & Replace while the blank lines between paragraphs are still there:
1. Rejoin words split by a hyphen at a line end
Find: -\n(?=[a-z])
Replace: (leave empty)
2. Turn single line breaks into spaces, keep blank lines
Find: ([^\n])\n(?=[^\n])
Replace: $1 (dollar sign, 1, then one space)Here's what that does to a typical paste:
Before:
This is a line that was
broken by the PDF and con-
tinues here.
New paragraph
here.
After:
This is a line that was broken by the PDF and continues here.
New paragraph here.Step 1 can't tell a split word from a real hyphenated compound that happened to break at a line end, so "well-" followed by "known" on the next line becomes "wellknown". Skim the result. Some PDFs also paste ligatures as single characters, so "fi" (U+FB01) and "fl" (U+FB02) turn up where "fi" and "fl" should be. A plain find and replace for each fixes them.
Text that arrives as HTML or Markdown
If you copied page source or a Markdown file, remove the markup before anything else. Strip HTML (in the converter's Encoding & hashing tab) deletes script and style blocks entirely, turns block tags such as paragraphs, list items, headings, table rows and <br> into line breaks so the structure survives, removes the remaining tags, and decodes common entities, so becomes an ordinary space rather than a no-break one.
Strip Markdown removes heading hashes, bold and italic markers, list and blockquote markers and horizontal rules, and reduces links and images to their text. One warning: it deletes fenced code blocks completely, contents included, so copy out any code you need first.
A clean-up order for messy pasted text
- Strip markup, if there is any.
- Delete zero-width characters and soft hyphens.
- Rejoin PDF lines while blank lines still mark the paragraphs.
- Normalise spaces: Clean whitespace if you don't need blank lines, the regex if you do.
- Straighten quotes, but only if the destination is code or data.
- Paste the original and the result into a diff checker and read the changes.
The order matters mostly because of step 4: Clean whitespace removes blank lines, and step 3 needs them. And the rule worth remembering: when two strings look identical but won't match, suspect an invisible character before you suspect a bug.


