textscrubr
Home / Blog / Developers

How to Remove Zero-Width Characters From a String

Developers2 min readUpdated 2026-10-25
To remove zero-width characters from a string, replace them by their code points: strip U+200B, U+200C, U+200D, U+2060, and U+FEFF. In JavaScript or Python a single regex replace does it. For a one-off, paste the string into a cleaner that removes zero-width and other invisible characters.

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:

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 joiners that emoji, Persian and Indic text need, so it does not break them.

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.

See what is hidden in your text

textscrubr shows every invisible character, removes the ones with no job, and keeps your lists, headings, links and code intact. Em dash cleanup is one optional toggle. Free, and it runs entirely in your browser.

Inspect and clean my text free →

Work inside Claude or ChatGPT? The same engine is available there for a one-time $29.

Frequently asked questions

What regex removes zero-width characters from a string?

In JavaScript, str.replace(/[\u200B-\u200D\u2060\uFEFF]/g, ''). In Python, re.sub(r'[\u200b\u200c\u200d\u2060\ufeff]', '', s). Exclude \u200D if you need to keep emoji joiners.

How do I remove all invisible format characters in Python?

Filter out characters in the Unicode 'Cf' category: ''.join(c for c in s if unicodedata.category(c) != 'Cf'). Use this when you want to strip all format characters, not just zero-width ones.

How do I strip zero-width characters without writing code?

Paste the string into a cleaner that removes invisible characters by code point. It strips zero-width spaces, joiners, and the byte-order mark while keeping the joiners real emoji need.