Toolsy

Invisible characters in copied text: what they are and what they break

Text copied from documents, PDFs and web pages carries characters you cannot see. A list of the usual ones, what each breaks, how to find them, and why deleting all of them is a mistake.

What a copied line really contains

Text is a sequence of characters, and some of them have no visible shape. Word processors, web pages, PDF viewers and CMS editors insert them for typesetting reasons: to allow or forbid a line break, to suggest where a word may be hyphenated, to mark the byte order of a file, or to control how letters join. When you copy text out of one of them, those characters travel with it.

Take the two words Hello world as they might arrive from a pasted document.

A phrase that contains two invisible charactersThe text Hello world drawn as twelve boxes. The sixth box is a zero width space and the seventh is a no-break space instead of a normal space. On screen it reads as 11 characters, in the text it is 12.HelloZWNBworldU+200B zero width spaceU+00A0 no-break spaceOn screen: Hello world, 11 characters.In the text: 12 characters, and not equal to the plain string.
Two characters you cannot see make the string 12 long and unequal to the plain 'Hello world'.

The usual suspects

Every one of these is a real character with its own code point. The names and categories below come from the Unicode character database.

Code point Name Shows as What it is for
U+200B zero width space nothing allows a line break with no visible space
U+200C zero width non-joiner nothing stops two letters from joining
U+200D zero width joiner nothing joins characters, including emoji
U+2060 word joiner nothing forbids a line break at that point
U+FEFF zero width no-break space nothing also the byte order mark at the start of a file
U+00AD soft hyphen nothing, or a hyphen at a line break marks where a word may be hyphenated
U+00A0 no-break space an ordinary space a space that forbids a line break
U+202F narrow no-break space a thin space a narrower no-break space
U+2007 figure space a wide space a space as wide as a digit

The first six are in Unicode category Cf (format characters), the last three in Zs (space separators). That difference matters in a moment.

What they break

None of these changes how the text looks. All of them change what a program sees. Four results, each checked in JavaScript or Python:

Test Result
'a​b' === 'ab' false, and the first one has length 3
'a b'.split(' ') one piece, because the no-break space is not a normal space
'computer' in 'com­puter' (Python) False, a search for the whole word finds nothing
encodeURIComponent('​') %E2%80%8B, which appears in a URL as junk

Trimming functions treat them differently, which is why a cleanup that worked in one language fails in another:

Character JavaScript trim() Python strip()
no-break space U+00A0 removed removed
zero width space U+200B kept kept
byte order mark U+FEFF removed kept

The byte order mark is the classic file-level case. A UTF-8 CSV saved with a BOM and read as plain utf-8 gives a first header of 'id' instead of 'id'; reading it as utf-8-sig gives 'id'. Every other column matches, and the first one does not.

The same exact-match logic reaches campaign links. Google’s help page says UTM values are case sensitive, which means they are matched exactly, so a value with a zero-width space inside should end up as a separate row that looks identical in the report. The UTM naming guide covers the visible half of that problem.

Finding them

You cannot see them, so ask the text for its code points. In JavaScript, with Unicode property escapes:

[...text]
  .filter(c => /[\p{Cf}\p{Zs}]/u.test(c) && c !== ' ')
  .map(c => 'U+' + c.codePointAt(0).toString(16).toUpperCase().padStart(4, '0'));

And in Python:

import unicodedata

[f'U+{ord(c):04X}' for c in text
 if unicodedata.category(c) in ('Cf', 'Zs') and c != ' ']

On the string 'Hello​ world “quoted” – dash ‎with mark' both print U+200B U+00A0 U+200E. The third one is the left-to-right mark, which belongs to the same Cf category but is not junk: it controls the direction of mixed text. So a list from the category is a list of candidates, not of things to delete.

Why you should not delete every invisible character

The tempting fix is a regular expression that removes everything in ​-‍. It is wrong for two kinds of text.

Emoji. A family emoji is not one character. It is three people joined by two zero width joiners: five code points, 1F468 200D 1F469 200D 1F467. Remove the joiners and what is left is three separate emoji, 👨👩👧.

Non-Latin scripts. In Persian, the zero width non-joiner is part of ordinary spelling. The word mikhaham is written with one inside it, and deleting it changes how the word is written.

This site’s own text cleaner removed every joiner until 30 October, so it would have split a family emoji and altered Persian words. It now removes a joiner only when it sits between plain Latin characters or at the edge of a word, and leaves it alone everywhere else. The zero width space, word joiner, byte order mark and soft hyphen are still always removed, since they carry no meaning in ordinary text.

Quotes, dashes and bullets

Copied text also brings visible characters that behave differently from the plain ones: the curly quotes U+2018, U+2019, U+201C and U+201D, the en dash U+2013 and the em dash U+2014, and the bullet U+2022. These are correct typography in an article and a problem in code, a CSV or a URL, where a straight quote is expected. Straighten them when the destination is a program and keep them when it is a reader.

Checklist

  • Text from a document, PDF or web page is checked before it goes into a CSV, a spreadsheet key, a URL or a campaign tag.
  • Detection goes by category, but nothing is deleted just because it is in Cf.
  • Joiners inside emoji and non-Latin words stay.
  • Files that start with a byte order mark are read with the right encoding.
  • Quotes and dashes are straightened only for destinations that need it.

The text cleaner removes the safe set in one pass and reports how many of each it found. The word and character counter counts what you can see, which is why a count and a length can disagree. The slug generator turns a phrase into a clean address, and the CSV to JSON converter is the next stop when the cleaned text is a table.