Invisible Unicode characters: diagnose before cleaning

Two strings can look identical while containing different code points. A practical example is invoice42 versus invoice42: the second value contains ZERO WIDTH SPACE U+200B. Strict comparison distinguishes them even when an editor gives no visual clue. That is useful evidence, but it does not mean every invisible character is unwanted.
Inspect this example in the cleaner
Start with the field contract
The same character needs different treatment in different data. A stock code, filename or database key may permit only a documented set of characters. Prose may use U+00A0 to keep a number and unit together, U+200D in joined emoji, or U+200C for correct word formation in some writing systems. Rejecting an unexpected code point can be right for the key and destructive for the sentence.
Make the failure reproducible
Keep the original and isolate a short sample. Reveal characters, record the code point and position, then compare length and code-point lists. Test the copy in the system that failed. This avoids blaming the first unusual character you notice. For example, Latin “a” and Cyrillic “а” are visible but confusable; they can also break equality and are not invisible-character cases.
Normalization is a separate decision
Unicode normalization can make a composed é comparable with e followed by a combining accent. It is not a general “remove invisibles” operation. NFKC also transforms compatibility forms, so it changes more data than NFC. Pick a form only when the receiving system defines one. Do not arbitrarily rewrite passwords, signatures, hashes or already-signed documents.
A safe cleaning workflow
- Save an unchanged copy.
- Inspect a small failing value and identify each character’s purpose.
- Choose the narrowest rule: remove a confirmed U+200B, map specific spaces, or preserve a joiner.
- Apply the rule to a copy and repeat the original test.
- Compare record counts, delimiters and meaning before and after.
For a bulk import, start with a representative sample. If content spans languages, emoji and formatted prose, use rules per field instead of a document-wide replacement.
What the cleaner cannot prove
A zero count means none of the supported categories was found. It does not identify an author, remove a universal “AI watermark” or prove source code safe. The tool also does not know the validation rules of your CRM, payment form or search index. The destination system remains the final test.
JavaScript example
const a = "invoice42";
const b = "invoice\u200B42";
a === b; // false
[...b].map(c => "U+" + c.codePointAt(0).toString(16).toUpperCase());If equality still fails after a justified removal, inspect case, normalization, spaces, combining marks and confusable letters from different scripts. Do not widen the rule until a new observation supports it.
A team-level regression check
Write the rule as an input-and-expected-output pair and keep it in importer or form tests. A stock code containing U+200B should either fail with a clear message or follow a documented transformation; a user sentence containing ZWJ should remain intact. Those two tests define the boundary better than a broad list of removed ranges. Repeat them when a parser, browser or export pipeline changes.
Sources and specifications
- Unicode Standard Annex #15: Normalization Forms
- W3C: Character Model for the World Wide Web — String Matching
- MDN: String.prototype.normalize()