θ·ͺζ‹œ Guibai
← All articles
前端 Β· JavaScript

JavaScript Length Lies: Why 'πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦' Counts as 11 and Your Han Regex Is Missing Half of China

By bai273 Β·
Read original on juejin.cn β†— Google Translate β†— Alt translation

Character counting in JavaScript is full of silent traps: .length counts UTF-16 code units, common Han regexes miss entire Unicode blocks, and property escapes fail without the u flag. These bugs can reject valid submissions, allow padding, and blow past title limits, all without a single error message.

Summary

A JavaScript word-count script used a hanzi regex of \u4e00-\u9fff, which only covers the CJK Basic block. When the script encountered an article padded with 200 characters from Extension B (like π €€), it counted 1000 instead of 1200 and failed the gate. The same regex also misses symbols like β‘  and fullwidth letters, which look like Chinese but aren't counted.

Measuring 14 samples with four counting methods shows the gap between .length, code points, graphemes, and Han matches. An emoji family sequence .length=11 but only 1 grapheme; a heart with variation selector is 2 code units; NFC and NFD forms of Γ© have different lengths. Title truncation platforms use .length, so a title with one emoji eats 10 extra code units, pushing it past the limit.

The fix uses Intl.Segmenter for grapheme count, .length for truncation, and \p{Script=Han} with the u flag for Han characters. The u flag is criticalβ€”without it, the regex silently matches literal text instead of erroring, a trap that's easy to miss.

Takeaways
β€” .length counts UTF-16 code units, not visual characters; an emoji family is 11 units but 1 grapheme.
β€” [...s].length counts code points, but still misses grapheme boundaries, so ZWJ sequences and combining marks inflate the count.
β€” The regex /[\u4e00-\u9fff]/ only matches the CJK Basic block; Extension A requires \u3400-\u4dbf, and Extension B and later need \u{...} with the u flag or \p{Script=Han}.
β€” Title length limits are enforced using .length, so a title with one emoji can consume 10 extra code units and hit the ceiling early.
β€” The same grapheme can have multiple code point forms (NFC/NFD); normalize strings before comparison or deduplication.
β€” Unicode property escapes like \p{Script=Han} must include the u flag; without it, the regex doesn't throw but matches literal text, silently returning zero hits.
β€” Punctuation and fullwidth characters (like β‘  and οΌ‘) take space in .length but are never counted as Han, so they can't pad a word count.
Conclusions

The silent failure of a missing u flag on property escapes is worse than an exception, because the regex matches literal text and produces false results without any warning.

CJK Extension blocks are poorly represented in common JavaScript regexes, yet they appear in real names, place names, and historical textsβ€”relying on the basic block alone is a correctness risk.

The same problem applies to any language using combining characters or non-BMP symbols; a character-count UI that naively uses .length will mislead users.

The gap between .length and grapheme count is not an edge case for emoji; it directly impacts title limits and content gates, where a few extra characters can block a publication.

Character counting is a domain where JavaScript's Unicode model forces developers to make a deliberate choice per use case, and mixing them up leads to errors of the scale "1200 vs 1000".

Concepts & terms
Grapheme cluster
A user-perceived character, which may consist of multiple code points, such as an emoji family joined by zero-width joiners or a letter with combining accents. Intl.Segmenter can split text into grapheme clusters.
UTF-16 code unit
The 16-bit unit that JavaScript's .length and string indexing operate on. Characters outside the Basic Multilingual Plane, like most emoji and CJK Extension B characters, are represented as surrogate pairs, taking two code units.
CJK Extension B
A Unicode block containing rare Han characters from U+20000 to U+2A6DF, which are outside the BMP and thus not covered by the common \u4e00-\u9fff regex.
Zero-width joiner (ZWJ)
A control character (U+200D) that connects multiple emoji characters into a single visual glyph, as in family emoji. It contributes to code unit and code point counts but not to the visual character count.
Unicode property escape
A regex feature like \p{Script=Han} that matches characters based on Unicode properties. It requires the u flag; without it, the escape is treated as a literal 'p' and can silently fail.
Source: juejin.cn β†— Google Translate β†— Backup β†—