How to Remove Hidden Characters From Text Safely
Strip zero-width spaces, BOMs and bidi controls with a clean copy, regex in JavaScript, Python or Go, and spreadsheet formulas, without breaking real text.

The short answer
There are two kinds of cleanup, and mixing them up is how text gets damaged.
- Cleaning a key: an email address, username, SKU, order number, URL, coupon code or anything you compare. Here you want plain characters only. Normalise with NFKC, delete every Unicode "format" character, collapse the spaces, trim. Nothing of value is lost.
- Cleaning prose: names, addresses, descriptions, messages, anything a person reads, especially in Hindi, Persian, Sinhala or with emoji. Here you delete only the characters that never carry meaning, and keep zero-width joiners where they sit between letters or emoji, and non-breaking spaces where they keep a price or a unit together.
For a single value, paste it into the Invisible Character Detector and copy the cleaned text. For a column of data or an application, use one of the functions below, which were all run against the same test strings before they went on this page.
If you still need to work out which characters are in your text, start with how to find invisible Unicode characters and come back.
Know what you are removing
These are the usual culprits and what to do with each:
| Code point | Name | In a key | In prose |
|---|---|---|---|
| U+200B | Zero-width space | Delete | Delete, except in Thai, Lao, Khmer or Burmese text |
| U+2060 | Word joiner | Delete | Usually delete |
| U+FEFF | Byte order mark | Delete | Delete |
| U+00AD | Soft hyphen | Delete | Delete, unless the text is typeset for print |
| U+202A to U+202E, U+2066 to U+2069 | Bidi controls | Delete | Delete, unless the text mixes right-to-left scripts |
| U+200C, U+200D | Zero-width non-joiner and joiner | Delete | Keep between letters or emoji, delete elsewhere |
| U+00A0, U+202F | Non-breaking spaces | Replace with a space | Keep in prices, units and dates, or replace |
The joiners are the dangerous row. Why they matter in emoji, Hindi and Persian, with pictures of what breaks, is in zero-width spaces explained.
Method 1: the clean copy in the Invisible Character Detector
Paste the text into the Invisible Character Detector and press Scan. Under the findings table there is a Cleaned text box and a Copy Cleaned Text button:
The clean copy deletes the zero-width space, both joiners, the word joiner, the byte order mark, the soft hyphen and the bidi overrides, and replaces non-breaking and typographic spaces with ordinary spaces. The text is processed on your device.
That is the right result for keys and for plain English. It is the wrong result for text with emoji sequences or Indian and Arabic scripts, because the clean copy removes every joiner, including the ones that are part of a word. For that text, use the code in Method 2, which keeps joiners where they belong. To confirm exactly what changed, paste the original and the cleaned copy into the text diff tool.
Method 2: regex and normalisation in code
Step 1: normalise first
Unicode allows the same visible text to be stored in more than one way, and normalisation picks one. There are two forms you will use:
- NFC composes characters into their standard single form, for example "e" plus a combining accent becomes "é". It changes nothing a reader can see. Use it for all stored text.
- NFKC does that and also folds "compatibility" characters into plain ones: a non-breaking space becomes a normal space, full-width letters become ASCII, the "fi" ligature becomes "fi", a superscript two becomes "2". Use it for keys and search, never for text you will display, because it changes what the reader sees.
Neither form removes zero-width characters. We checked: NFKC leaves U+200B, U+200C, U+200D, U+2060 and U+FEFF exactly where they were. Normalisation is a first step, not the cleanup.
Step 2: cleaning a key
For identifiers, normalise with NFKC, delete the whole Cf (format) category, then collapse and trim the spaces. Cf covers the zero-width characters, the byte order mark, the soft hyphen, the bidi controls and the tag characters in one class.
JavaScript:
const cleanKey = (value) =>
value.normalize("NFKC").replace(/\p{Cf}/gu, "").replace(/\s+/g, " ").trim();
cleanKey(" priya\u200C@example.in\u00A0"); // "priya@example.in"
Python:
import unicodedata
def clean_key(value):
value = unicodedata.normalize("NFKC", value)
value = "".join(c for c in value if unicodedata.category(c) != "Cf")
return " ".join(value.split())Go, with golang.org/x/text/unicode/norm:
func CleanKey(s string) string {
s = strings.Map(func(r rune) rune {
if unicode.Is(unicode.Cf, r) {
return -1 // drop it
}
return r
}, norm.NFKC.String(s))
return strings.Join(strings.Fields(s), " ")
}Never run this on prose. It turns the Hindi half form into a different letter and splits emoji families into separate faces.
Step 3: cleaning prose without breaking it
For text people read, delete the characters that never carry meaning, then remove a joiner only when it is not sitting between two letters, combining marks or emoji. A joiner next to a space, a digit, punctuation or the edge of the string is junk. This keeps the joiners in Hindi, Persian and emoji, and still catches the stray one at the end of a pasted name.
JavaScript:
const ALWAYS_DROP = /[\u200B\u2060\uFEFF\u00AD\u202A-\u202E\u2066-\u2069]/g;
const JOINABLE = /[\p{L}\p{M}\p{Extended_Pictographic}]/u;
function cleanText(text) {
const chars = [...text.normalize("NFC").replace(ALWAYS_DROP, "")];
return chars
.filter((c, i) => {
if (c !== "\u200C" && c !== "\u200D") return true;
return JOINABLE.test(chars[i - 1] ?? "") && JOINABLE.test(chars[i + 1] ?? "");
})
.join("");
}Python:
import re
import unicodedata
ALWAYS_DROP = re.compile(r"[\u200B\u2060\uFEFF\u00AD\u202A-\u202E\u2066-\u2069]")
def joinable(ch):
if not ch:
return False
cat = unicodedata.category(ch)
return cat[0] in "LM" or cat == "So"
def clean_text(text):
text = ALWAYS_DROP.sub("", unicodedata.normalize("NFC", text))
out = []
for i, ch in enumerate(text):
if ch in "\u200c\u200d":
before = text[i - 1] if i > 0 else ""
after = text[i + 1] if i + 1 < len(text) else ""
if not (joinable(before) and joinable(after)):
continue
out.append(ch)
return "".join(out)Go:
func alwaysDrop(r rune) bool {
switch {
case r == 0x200B, r == 0x2060, r == 0xFEFF, r == 0x00AD:
return true
case r >= 0x202A && r <= 0x202E, r >= 0x2066 && r <= 0x2069:
return true
}
return false
}
func joinable(r rune) bool {
return unicode.IsLetter(r) || unicode.IsMark(r) || unicode.Is(unicode.So, r)
}
func CleanText(s string) string {
var kept []rune
for _, r := range norm.NFC.String(s) {
if !alwaysDrop(r) {
kept = append(kept, r)
}
}
var b strings.Builder
for i, r := range kept {
if r == 0x200C || r == 0x200D {
if i == 0 || i == len(kept)-1 || !joinable(kept[i-1]) || !joinable(kept[i+1]) {
continue
}
}
b.WriteRune(r)
}
return b.String()
}Here are the Python and Go versions run on the same test strings. The zero-width space, the byte order mark, the stray joiner before a space and the bidi override all go. The non-breaking space in the price, the joiner in the Hindi half form and the joiner in the emoji all stay:
Notice what these functions do not touch: the non-breaking space. In prose it is usually intentional. If you want it gone in a particular field, replace \u00A0 and \u202F with a normal space as a separate, deliberate step. To try a pattern on your own text before you put it in code, paste both into the regex tester.
Method 3: Excel and Google Sheets
Spreadsheets are where hidden characters do the most quiet damage, because a lookup that fails returns #N/A and nobody checks why.
What CLEAN and TRIM actually remove
- CLEAN removes the first 32 ASCII control codes, 0 to 31, such as tabs and line feeds. It does not remove the non-breaking space (160), the zero-width space (8203) or the byte order mark (65279). Microsoft documents this limit.
- TRIM removes ordinary spaces (code 32) from the ends and collapses runs of them in the middle. In Excel it leaves non-breaking spaces alone, which is why
TRIM"does nothing" to text pasted from a web page, as the TRIM documentation notes.
So =TRIM(CLEAN(A2)), the formula most guides recommend, leaves exactly the characters this page is about.
Excel: SUBSTITUTE with UNICHAR
Swap the non-breaking space for a normal one and delete the zero-width characters by number, then trim:
=TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,UNICHAR(160)," "),UNICHAR(8203),""),UNICHAR(65279),""),UNICHAR(8288),"")))The numbers are decimal code points: 160 is U+00A0, 8203 is U+200B, 65279 is U+FEFF and 8288 is U+2060. Add another SUBSTITUTE for any other character you found, for example 173 for the soft hyphen. UNICHAR exists from Excel 2013 onwards.
To check whether a cell still hides something, compare =LEN(A2) with the length you can count. If they differ, the cell is not clean.
Google Sheets: REGEXREPLACE
Sheets has regular expressions, so one formula covers the whole set:
=TRIM(REGEXREPLACE(A2,"[\x{200B}-\x{200D}\x{2060}\x{FEFF}\x{00AD}]",""))Wrap it in SUBSTITUTE(..., CHAR(160), " ") if you also want non-breaking spaces replaced. This formula removes joiners too, so use it on code, email and ID columns, not on a column of names in Indian or Arabic scripts.
To count the hidden characters in a cell before cleaning:
=LEN(A2)-LEN(REGEXREPLACE(A2,"[\x{00A0}\x{00AD}\x{200B}-\x{200D}\x{2060}\x{FEFF}]",""))Method 4: VS Code and the command line
In VS Code, open Find and Replace, switch on regular expressions (the .* button), search for [\u200B\u2060\uFEFF\u00AD] and replace with nothing. Use Find in Files with the same pattern to clean a whole project, and review the diff before you commit.
A byte order mark at the start of a file shows in the status bar as "UTF-8 with BOM". Click it, choose Save with Encoding, then UTF-8, and the mark is gone.
On the command line, Perl handles every case and is installed on macOS and almost every Linux server:
perl -CSD -pi -e 's/[\x{200B}\x{2060}\x{FEFF}\x{00AD}]//g' file.txtThe find and replace tool does the same in a browser: tick the regex option, use [\u200B\u2060\uFEFF] as the pattern and leave the replacement empty. Follow it with the whitespace cleaner to trim line edges and collapse the doubled spaces that removal can leave behind.
Checklist: what to keep
Before you run any cleanup over real data, go through this list:
- Non-breaking spaces in prices, units and dates. "Rs 499", "6 pm" and "12 kg" use them so the number never wraps away from its unit. Keep them in display text, replace them only in keys.
- Joiners in Indian scripts. A ZWJ or ZWNJ between Devanagari, Sinhala or Malayalam letters is spelling.
- ZWNJ in Persian, Urdu and other Arabic-script text. It is standard orthography.
- Joiners and variation selectors in emoji. U+200D between emoji and U+FE0F after a symbol are part of the emoji.
- Zero-width spaces in Thai, Lao, Khmer and Burmese. They may be the only word boundaries.
- Escaped characters in source code. A
\u200Dwritten as an escape inside a string literal is deliberate. A raw invisible character in code is not.
And what to remove without a second thought: anything invisible in an email address, username, password field, SKU, URL, API key, CSV header or database key, and every byte order mark that is not the first character of a file.
Fix it once, at the door
Cleaning a spreadsheet by hand works once. The lasting fix is to normalise and validate text where it enters your system: cleanKey on every identifier field, cleanText on free text, at every form and every import, so bad characters never reach the database in the first place. Adding that kind of input hardening and cleaning up the data already collected is part of the web application work we do. For a single file or a single column, the tools and formulas above are enough.
Tools mentioned in this post
- Invisible Character DetectorFind the zero-width spaces, hidden Unicode and look-alike letters in any text, see exactly where they are, and copy a clean version.
- Find & ReplaceReplace every occurrence in one pass: plain text or regex, case-sensitive or not.
- Whitespace CleanerStrip messy spacing: trim line edges, collapse double spaces, drop empty lines.
- Regex TesterTest JavaScript regular expressions live: matches, indices and capture groups as you type.
- Text Diff CheckerCompare two texts line by line: additions and deletions highlighted.


