Why `"SKU42" === "SKU42"` Can Be False When One Value Is Pasted

A spreadsheet lookup fails for SKU42. You can see SKU42 in the product list, and copying the value into a scratch file doesn’t reveal anything unusual. The two strings look the same.
Here’s one way to reproduce that problem in JavaScript:
const expected = "SKU42"; const pasted = "SKU\u200B42"; console.log(pasted); // Looks like SKU42 console.log(pasted === expected); // false The \u200B escape inserts a zero-width space between the U and the 4. It adds no visible space, but JavaScript still sees an extra character when it compares the strings.
Inspect the code points
When a pasted string looks right but fails an exact match, print its code points alongside those of a known-good value:
for (const value of [expected, pasted]) { const codePoints = Array.from(value, (char) => { const hex = char.codePointAt(0).toString(16).toUpperCase(); return `U+${hex.padStart(4, "0")}`; }); console.log(codePoints.join(" ")); } The extra character is easy to spot in the output:
U+0053 U+004B U+0055 U+0034 U+0032 U+0053 U+004B U+0055 U+200B U+0034 U+0032 A common first move is to try .trim(). JavaScript’s trim() operates at the ends of a string, and U+200B is outside the set of characters it removes. For this SKU, you can remove the character directly and rerun the comparison:
const cleaned = pasted.replaceAll("\u200B", ""); console.log(cleaned === expected); // true Keep the cleanup specific to the field
If your SKU format only allows letters and digits, removing an accidental zero-width space from a pasted value can make sense. Applying a blanket cleanup to every text field can change content people intended to keep.
For example, 👩💻 uses a zero-width joiner (U+200D) between the woman and laptop characters. Removing that joiner can turn the combined emoji into 👩💻. Even U+200B has a legitimate use. It can mark a place where a line is allowed to wrap without adding a visible gap.
For a quick check in the browser, I work on a tool that shows invisible characters, their code points, and their positions. You can select which characters to remove and inspect the cleaned result before copying it. TextBench Invisible Character Detector
The next time a pasted value fails an exact match, compare its code points with a known-good value before changing your matching logic. A single extra character can explain the whole failure.
原文:https://www.a8q9.cn/en/article/why-sku42-sku42-can-be-false-when-one-value-is-pasted-ea51e0.html