Zero Width Space: find U+200B and remove only what is unwanted

Topic illustration for the article: Zero Width Space: find U+200B and remove only what is unwanted
Practical illustration for this guide

ZERO WIDTH SPACE, or ZWSP, is U+200B. It draws no ordinary blank but can provide a line-break opportunity where no visible word boundary exists. A copy from a PDF, web interface or layout system may carry it into a product code, address, query or form value, making two apparently equal strings different.

Inspect the U+200B example

A stock-code failure

Suppose an application expects SKU-42, while a catalogue supplies SKU-​42. The screen hides the difference, but an exact comparison, unique database index or request signature includes U+200B. Here the contract allows Latin letters, digits and a hyphen, so the ZWSP is clearly outside the field’s alphabet. Confirm its position and remove that character only.

When U+200B is intentional

In text without visible spaces, ZWSP can give a layout engine another place to wrap. Authors may also insert it into a long path or compound label to prevent overflow. Removing every U+200B from an entire publication may make wrapping worse. Prose needs an editorial decision; a structured identifier should follow its validation specification.

Do not group every zero-width character together

U+200B differs from ZERO WIDTH NON-JOINER U+200C, ZERO WIDTH JOINER U+200D and U+FEFF. Joiners can affect words and emoji sequences. U+FEFF at the beginning of a decoded stream may represent a byte-order mark, while the same code point inside content is data. A rule described only as “delete zero width” is too broad to review safely.

Step-by-step diagnosis

  1. Preserve the original value and the error context.
  2. Reveal characters and verify that the finding is U+200B.
  3. Check whether the field allows break opportunities or only ASCII.
  4. Remove U+200B from a copy; do not insert a space without evidence.
  5. Repeat the import, lookup or comparison and inspect neighbouring records.

For batch work, count changed rows and log the transformation. A surprising number of matches may indicate that the source is formatted prose and should be exported differently.

Why replacement with a space can be wrong

Turning SKU-​42 into SKU- 42 creates another invalid stock code. Inside a word, it can split one token into two. An empty replacement is common when U+200B was only a wrap hint; if it represented an accidental word boundary, a human must reconstruct the intended text.

JavaScript example

const value = "SKU-\u200B42";
value === "SKU-42"; // false
value.replaceAll("\u200B", "") === "SKU-42"; // true

Use that replacement only after deciding that U+200B is forbidden throughout the target field. For mixed user prose, preview the changes or limit processing to a known source.

Open the cleaner

Test the boundary of the rule

Use three values: a clean stock code, one containing U+200B and prose where the character provides a wrap opportunity. Define the expected result separately. If one function changes all three in the same way, its scope is too broad. A server validator should preferably report the code point and position instead of silently rewriting input, so the producing system can be corrected.

Sources and specifications

Related guides