How to Find and Remove Invisible Unicode Characters
Zero-width spaces, BOMs, look-alike letters and direction overrides: why invisible characters break code, and how to find and remove them safely.
Two strings look identical on screen, but === says they're different. A username passes validation but can't log in. A test that compares dates fails only on one machine. A JSON file won't parse, and the error points at a line that looks perfectly fine.
Very often the cause is a character you can't see. Unicode has dozens of them: zero-width spaces, joiners, direction marks, special spaces, and letters from other alphabets that look exactly like Latin ones. This guide shows which ones cause trouble, how to find them, and how to clean them out safely.
Why invisible characters exist
None of these characters are bugs in Unicode. Each one has a real job:
- Zero-width space (U+200B) marks where a long word may break across lines, without showing a space.
- Zero-width joiner (U+200D) glues emoji together: 👩 + ZWJ + 💻 makes 👩💻. It's also essential in scripts such as Arabic and the Indian scripts.
- Non-breaking space (U+00A0) keeps "10 km" or "Mr. Lee" on one line.
- Direction marks and embeddings (U+200E, U+200F, U+202A–U+202E, U+2066–U+2069) control how right-to-left text, such as Arabic or Hebrew, mixes with left-to-right text.
- The byte order mark (U+FEFF) was meant to label a file's encoding, and some editors still put it at the start of UTF-8 files.
The trouble starts when these characters travel somewhere they weren't meant to go: copied from a web page, a PDF, a word processor or a chat app, into code, configuration, a database or a form.
The usual suspects
| Character | Code point | Where it comes from | What it breaks |
|---|---|---|---|
| Zero-width space | U+200B | Web pages, CMS editors, copied URLs | String comparison, search, URLs, usernames |
| Zero-width joiner / non-joiner | U+200D / U+200C | Emoji, some scripts | Comparisons, when they appear outside emoji or those scripts |
| Byte order mark | U+FEFF | Windows editors, exported CSV files | The first column name of a CSV, #! lines, JSON parsing |
| Non-breaking space | U+00A0 | Word processors, web pages, macOS (⌥ Space) | Shell commands, split(' '), YAML, Python indentation |
| Narrow no-break space | U+202F | Date and time formatting | Tests comparing formatted times |
| Soft hyphen | U+00AD | Word processors, typesetting | Search, comparisons |
| Direction overrides | U+202A–U+202E, U+2066–U+2069 | Mixed-direction text, or malicious code | What code appears to do (see Trojan Source below) |
| Tag characters | U+E0000–U+E007F | Flag emoji, or text deliberately hidden | Hidden instructions in text given to AI tools |
The time-format surprise
That narrow no-break space has broken plenty of test suites. Unicode's shared locale data (CLDR 42, from 2022) switched English times to use U+202F, not a normal space, before "AM" and "PM", and software that adopted the change started producing it. Browsers and Node.js have since gone back to a plain space, but the character still turns up: in times formatted by recent Java versions, for example, and in macOS screenshot file names ("Screenshot … at 9.30.00 AM.png"). A script that builds the expected name with a normal space never finds the file.
// a time formatted elsewhere, with U+202F before "AM"
const time = '9:30\u202FAM';
time === '9:30 AM'; // false
time.replace(/\u202F/g, ' ') === '9:30 AM'; // true
Nothing looks different when you print it. Compare against the formatter's own output, or normalise spaces before comparing.
Look-alike letters (homoglyphs)
Some characters aren't invisible, just impostors. The Cyrillic letter "а" (U+0430) looks exactly like the Latin "a" (U+0061) in almost every font. The same goes for Cyrillic "е", "о", "р", "с" and "х", and Greek "ο" and "ν".
That makes look-alikes useful for tricks:
- Phishing domains that read as a familiar brand, but contain one Cyrillic letter. Browsers defend against this by showing such domains as
xn--…(Punycode) when scripts are mixed. - Fake usernames that look like an admin's or a well-known account's.
- Spam filters dodged by swapping a few letters in banned words.
The important nuance: Cyrillic or Greek letters are completely normal in Russian or Greek text. What's suspicious is a single word that mixes Latin letters with look-alikes from another script. That's exactly what the Unicode inspector flags, so ordinary Russian or Greek text stays clean.
Trojan Source: invisible characters in code
In 2021, researchers at the University of Cambridge showed that bidirectional control characters could make source code display differently from how the compiler reads it (CVE-2021-42574, nicknamed "Trojan Source"). A right-to-left override placed inside a comment or a string can reorder what you see, so a line that looks like a harmless comment is really code that runs, or the other way round.
Code review happens on screen, and the screen is exactly what these characters manipulate. Since then:
- GitHub warns when a file contains hidden bidirectional characters.
- VS Code highlights invisible and ambiguous characters by default (
editor.unicodeHighlight.*settings). - Compilers caught up: Rust rejects them in code by default, and GCC warns about them (
-Wbidi-chars).
It's still worth checking code pasted from blogs, forums or AI answers before you commit it.
Hidden text for AI tools
Unicode tag characters (U+E0000–U+E007F) were designed for language tags, and today their only everyday use is in subdivision flags such as 🏴 (Scotland). Each tag character mirrors an ASCII character, and almost nothing displays them.
That means a sentence can carry a second, invisible sentence spelt in tag characters. People have used this to hide instructions for AI assistants inside web pages, emails and documents ("ASCII smuggling"): the reader sees nothing, but a model that receives the raw text may read and follow it.
The inspector decodes stray tag characters and shows the hidden text, while leaving genuine flag emoji alone.
How to find invisible characters
In the browser
Paste the text into the Unicode inspector. It lists every character with its code point, name and UTF-8 bytes, and calls out:
- invisible and zero-width characters;
- unusual spaces;
- direction controls;
- look-alike letters in mixed-script words;
- hidden tag text.
Clean up then removes the invisible, direction and stray tag characters, turns special spaces into plain ones, and normalises the text to NFC. It's a normal edit, so Ctrl/⌘Z brings the original back. Everything happens in your browser, so you can paste private text.
You can also paste suspicious text into the toolkit's search (Ctrl/⌘K). It notices hidden characters and offers to open the inspector.
In JavaScript
Unicode property escapes (with the u or v flag) let a regex match whole categories. Cf is the "format" category, which covers most invisible characters:
// list the invisible format characters in a string
function hiddenCharacters(text) {
return [...text.matchAll(/\p{Cf}/gu)].map((match) => ({
index: match.index,
code: 'U+' + match[0].codePointAt(0).toString(16).toUpperCase().padStart(4, '0'),
}));
}
hiddenCharacters('pass\u200Bword'); // [{ index: 4, code: 'U+200B' }]
Be careful when removing Cf characters: the zero-width joiner is in that category too, and stripping it breaks emoji sequences such as 👩💻 and words in some scripts.
In Python
import unicodedata
def hidden_characters(text: str):
for index, char in enumerate(text):
if unicodedata.category(char) in ("Cf", "Zs") and char != " ":
yield index, f"U+{ord(char):04X}", unicodedata.name(char, "unknown")
list(hidden_characters("10\u00a0km")) # [(2, 'U+00A0', 'NO-BREAK SPACE')]
On the command line
With GNU grep's Perl-compatible mode:
# zero-width characters, word joiner and byte order mark
grep -nP '[\x{200B}-\x{200D}\x{2060}\x{FEFF}]' file.txt
# direction controls (Trojan Source)
grep -rnP '[\x{202A}-\x{202E}\x{2066}-\x{2069}]' src/
# any non-ASCII character at all
grep -nP '[^\x00-\x7F]' file.txt
Same letters, different bytes: normalisation
There's one more kind of invisible difference. "é" can be stored as one character (U+00E9) or as "e" followed by a combining accent (U+0065 U+0301). They look the same, and they are different strings:
const composed = '\u00e9';
const decomposed = 'e\u0301';
composed === decomposed; // false
composed.length; // 1
decomposed.length; // 2
composed.normalize('NFC') === decomposed.normalize('NFC'); // true
Normalise text to NFC before you compare it, store it as a key, or check it for duplicates. Usernames, file names and search indexes all benefit. (Text copied from older macOS file names is a classic source of the decomposed form.)
Preventing problems
- Validate identifiers strictly. For usernames, slugs and codes, allow a known set of characters rather than trying to block the bad ones.
- Normalise on the way in. Apply NFC, and decide what to do with special spaces, when text enters your system, not each time you compare it.
- Keep invisible characters out of your source code. Write them as escapes (such as
\u200B) so reviewers can see them. ESLint'sno-irregular-whitespacerule, on by default in its recommended set, catches odd spaces in JavaScript. - Save files as UTF-8 without a BOM, unless a tool specifically needs one. (Excel, notably, uses the BOM to recognise UTF-8 CSV files.)
- Treat pasted text as untrusted, especially text you'll feed to an AI tool or run as code.
Summary
- Invisible characters, special spaces and look-alike letters each have a legitimate job, and cause bugs when they leak into code, data and identifiers.
- The worst offenders are zero-width characters, the BOM, non-breaking and narrow spaces, direction controls, and tag characters that hide text.
- Find them with a regex on
\p{Cf}, Python'sunicodedata,grep -P, or the Unicode inspector, which also cleans them out with one undoable click.