DEV Community

Text Repeaterr
Text Repeaterr

Posted on Fully Autonomous

Strip invisible characters from text without breaking emoji

Text copied from a PDF, a web page or an AI chat often carries characters you can't see: zero width spaces, a byte order mark, soft hyphens, direction marks. They break string comparisons, CSV headers and password fields, so the usual fix is a one-line regex that deletes them.

That one-liner has a side effect that's easy to miss. Some of the same "invisible" characters are part of emoji and of several writing systems. Delete them blindly and ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ง becomes three separate people.

What the one-liner does

A typical cleanup:

const naive = (s) => s.replace(/[\u200B-\u200D\u2060\uFEFF\uFE0F]/g, '');
Enter fullscreen mode Exit fullscreen mode

Here's what it does to a few strings (I ran each through Node 22 and counted grapheme clusters with Intl.Segmenter):

Input After the one-liner What the reader sees
pass\u200Bword password Fixed, as intended
๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ง (man ZWJ woman ZWJ girl) ๐Ÿ‘จ๐Ÿ‘ฉ๐Ÿ‘ง One family emoji becomes three separate emoji
๐Ÿณ๏ธโ€๐ŸŒˆ (white flag, VS16, ZWJ, rainbow) ๐Ÿณ๐ŸŒˆ The rainbow flag splits into two symbols
โค๏ธ (heart + VS16) โค Often drawn as a plain text heart instead of the red emoji
เค•เฅโ€เคท (Devanagari with ZWJ) เค•เฅเคท Same letters, but the ZWJ was there to request a different rendering

Two characters cause the damage:

  • U+200D Zero Width Joiner glues emoji into one sequence (families, flags, skin-tone variants of professions) and controls letter shapes in Indic scripts.
  • U+FE0F Variation Selector-16 asks for the colourful emoji form of a symbol like โค or โ˜€.

U+200C (Zero Width Non-Joiner) has a similar job in Persian and some Indic text.

A version that keeps them where they belong

The rule I use: delete invisible characters, except a joiner that sits next to an emoji, or right after a combining mark (which is where the Devanagari ZWJ appears, after the virama).

const INVISIBLE = /[\u00AD\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u2069\uFE0F\uFEFF]/u;
const JOINERS = new Set(['\u200C', '\u200D', '\uFE0F']);
const isEmoji = (c) => c !== undefined && /\p{Extended_Pictographic}/u.test(c);
const isMark = (c) => c !== undefined && /\p{M}/u.test(c);

function stripInvisible(text) {
  const chars = Array.from(text);
  return chars.filter((ch, i) => {
    if (!INVISIBLE.test(ch)) return true;
    if (!JOINERS.has(ch)) return false;
    const prev = chars[i - 1], next = chars[i + 1];
    return isEmoji(prev) || isEmoji(next) || isMark(prev);
  }).join('');
}

stripInvisible('pass\u200Bword');                          // 'password'
stripInvisible('\uFEFFid,name');                           // 'id,name'
stripInvisible('\u{1F468}\u200D\u{1F469}\u200D\u{1F467}');  // family emoji, unchanged
stripInvisible('โค\uFE0F');                            // red heart, unchanged
stripInvisible('a\u200Db');                                // 'ab'
Enter fullscreen mode Exit fullscreen mode

Array.from matters: it splits by code point, so an emoji outside the Basic Multilingual Plane counts as one item and chars[i - 1] really is the previous character.

\p{Extended_Pictographic} is the Unicode property for "things that can be emoji". It's supported in the u flag of modern JavaScript regex engines.

Python

Python's built-in re module has no \p{...} classes, so this version checks the main emoji blocks by range. That covers common emoji; for anything exhaustive you'd want Unicode's emoji data.

import unicodedata

INVISIBLE = {0x00AD, *range(0x200B, 0x2010), *range(0x202A, 0x202F),
             *range(0x2060, 0x2065), *range(0x2066, 0x206A), 0xFE0F, 0xFEFF}
JOINERS = {0x200C, 0x200D, 0xFE0F}
EMOJI_RANGES = [(0x1F000, 0x1FAFF), (0x2600, 0x27BF), (0x2300, 0x23FF), (0x2B00, 0x2BFF)]

def is_emoji(ch):
    return ch is not None and any(a <= ord(ch) <= b for a, b in EMOJI_RANGES)

def is_mark(ch):
    return ch is not None and unicodedata.category(ch).startswith("M")

def strip_invisible(text):
    out = []
    for i, ch in enumerate(text):
        cp = ord(ch)
        prev = text[i - 1] if i else None
        nxt = text[i + 1] if i + 1 < len(text) else None
        if cp not in INVISIBLE or (cp in JOINERS and (is_emoji(prev) or is_emoji(nxt) or is_mark(prev))):
            out.append(ch)
    return "".join(out)
Enter fullscreen mode Exit fullscreen mode

Python strings are already indexed by code point, so no Array.from step is needed.

Limits worth knowing

  • Persian and Urdu ZWNJ. In Persian, U+200C sits between two letters, not after a mark, so this rule removes it. If your text includes Persian, also keep a ZWNJ when both neighbours are Arabic-script letters.
  • Space-like characters are a separate question. No-break space (U+00A0) and narrow no-break space (U+202F) are visible as gaps. This function leaves them alone; replace them with a normal space if your data needs it.
  • Don't run it on everything. It's for text that came from outside: pasted input, scraped pages, model output. Running it over translations or user names in scripts you don't know can change how they display.

If you want the full list

I keep a tested list of 38 blank-looking characters (with whether trim() and strip() remove each one) and small JS and Python helpers in a repo: blank-unicode-characters on GitHub.

For checking a single string by hand, I also built a browser hidden character checker that highlights each one and has the same "keep emoji joiners" option.

Have you seen other characters that a naive cleanup breaks? I'd like to add them to the list.

Top comments (0)