Zero-width characters inside a string cause bugs that are invisible in your editor: failed comparisons, broken parsing, and mismatched keys. Here is how to strip them.
Which characters to target
The common zero-width and invisible characters:
U+200Bzero-width spaceU+200Czero-width non-joinerU+200Dzero-width joinerU+2060word joinerU+FEFFbyte-order mark / zero-width no-break space
In JavaScript
A single regex replace removes them:
const clean = str.replace(/[\u200B-\u200D\u2060\uFEFF]/g, '');
If you need to keep the zero-width joiner for emoji, exclude \u200D from the range and handle it separately.
In Python
import re
clean = re.sub(r'[\u200b\u200c\u200d\u2060\ufeff]', '', s)
Or, to strip a broader set of invisible and format characters, remove characters in the Unicode "Cf" (format) category:
import unicodedata
clean = ''.join(c for c in s if unicodedata.category(c) != 'Cf')
That is a heavier hammer, so use it when you want to remove all format characters, not just the zero-width ones.
For a one-off, use a cleaner
If you just have a string to fix and do not want to write code, paste it into a cleaner that strips invisible characters by code point. textscrubr removes zero-width spaces, joiners, the word joiner, and the byte-order mark, shows you a count of what it found, and keeps the zero-width joiners that real emoji need, so it does not break your emoji.
Verify
After stripping, compare the length before and after, or re-scan for the code points. If the count drops and a re-scan finds nothing, the string is clean. Adding this step to input handling or a data pipeline stops zero-width characters from ever reaching comparisons and parsers.