Skip to main content

Guides

Invisible characters in text

· 7 min read

Two strings look identical on screen, and every comparison says they differ. A search finds nothing though the word is visibly there. A code block will not run and the error points at a character you cannot see. All of these have the same cause.

The usual suspects

CharacterWhere it comes fromWhat it breaks
Non-breaking spaceWord processors, web pages, PDFsSplitting on spaces, trimming, comparisons
Zero-width spaceCopying from web pages and chat appsSearch, comparison, character counts
Byte order markFiles saved as UTF-8 with BOMThe first line of a config or CSV
Smart quotesWord processors and editors that autocorrectCode, CSV parsing, exact matching
Soft hyphenJustified text, PDFsSearch, word matching
Non-standard whitespaceCopied tables and formatted outputSplitting and trimming

Why a non-breaking space is the worst of them

It looks exactly like a space, it is wide like a space, and it is not a space. Code that splits on a space will not split there. A trim that removes whitespace may or may not remove it depending on the language and the function. A comparison against a normal space fails, and nothing about the screen explains why.

It arrives whenever text passes through something that cares about typography: a word processor keeping a number next to its unit, a web page preventing an awkward line break, a PDF preserving layout.

Byte order marks and the first line

A BOM is three bytes at the very start of a file. Many editors hide it. The symptom is that the first column header of a CSV does not match, or the first key of a config is not found, while every other line works. If a problem is confined to the first line of a file, suspect a BOM before anything else.

Smart quotes in code and data

A word processor turns straight quotes into curly ones. In prose that is correct typography. In a code snippet it produces a syntax error, and in a CSV it breaks quoting rules, because a curly quote is an ordinary character rather than a delimiter.

If a snippet copied from a document, a slide or a chat message will not run, check the quotes before anything else. It is the most common cause and the least visible.

How to find them

When to strip and when not to

For data that will be compared, parsed, stored as a key or used in a URL, normalise: convert every kind of space to a plain space, straighten quotes, remove zero-width characters and the BOM. For prose you are going to publish, be more careful. Non-breaking spaces are often deliberate, and removing them changes where lines break.

The cleaner here runs in the browser, which matters because the text being cleaned is often a pasted email or document rather than something public.

Related: How to clean AI-generated text before you publish it

Frequently asked questions

Why do two identical-looking strings not match?

Almost always an invisible character: a non-breaking space, a zero-width space or a curly quote in one of them. Compare the character counts first; a difference confirms it.

What is a BOM and why does it break only the first line?

It is three bytes at the very start of a UTF-8 file that most editors hide. It attaches to the first field or key, so everything after the first line parses correctly.

Should I always remove non-breaking spaces?

In data, yes. In prose intended for publication, no. They are often deliberate typography, and removing them changes where lines break.