跪拜 Guibai
← Back to the summary

JavaScript Length Lies: Why '👨‍👩‍👧‍👦' Counts as 11 and Your Han Regex Is Missing Half of China

The length of "👨‍👩‍👧‍👦" is 11: I dragged my word-count script out for an audit

Three conclusions first:

  1. To count "how many characters", use grapheme clusters (Intl.Segmenter); to check "will it be truncated", use .length (UTF-16 code units). These two numbers often differ by several times.
  2. The Han character regex \u4e00-\u9fff misses the entire CJK extension blocks. My own gate script was written this way, so it judged a 1200-character article as 1000 characters.
  3. Title length limits are based on .length. If you stuff an emoji into a title, 19 code units actually render as only 9 visual characters—you think you have room for 10 more characters, but you've already hit the cap.

Here's the process of measuring it, on my machine: Windows 11 + Node v22.22.2. The script and raw output are retained.

Origin: I have a gate that requires "article body ≥ 1200 characters"

In my writing workflow, there's a hard gate: the body must have at least 1200 Han characters before publishing. The script is short; the core is four lines:

t = t.replace(/```[\s\S]*?```/g, '');     // remove fenced code blocks
t = t.replace(/`[^`\n]*`/g, '');          // remove inline code
const cjk = (t.match(/[\u4e00-\u9fff]/g) || []).length;
const total = [...t.replace(/\s/g, '')].length;

It ran for days without problems, until I started using emoji in titles and bodies. One day, an article was clearly long, but the gate reported PASS=NO. My first instinct was that the script was wrong—but "the script is wrong" can't be a conclusion; I had to measure.

Step 1: Take 14 samples, run four counting methods at once

For the same strings, I calculated .length, code point count [...s].length, grapheme count (via Intl.Segmenter, granularity grapheme), and hits from two Han regexes:

Sample .length [...s] Graphemes \u4e00-\u9fff \p{Script=Han}
一 1 1 1 1 1
𠀀 (U+20000) 2 1 1 0 1
𠮷 (U+20BB7) 2 1 1 0 1
👍 2 1 1 0 0
👨‍👩‍👧‍👦 11 7 1 0 0
❤️ (U+2764 U+FE0F) 2 2 1 0 0
é (NFC, U+00E9) 1 1 1 0 0
é (NFD, e+U+0301) 2 2 1 0 0
① 1 1 1 0 0
㈠ 1 1 1 0 0
㊙ 1 1 1 0 0
한 1 1 1 0 0
A (fullwidth) 1 1 1 0 0
①𠀀👍 mixed 5 3 3 0 1

This table carries more information than I thought. Let me go through it.

The "family of four" emoji: .length 11, graphemes 1. It's four person emoji joined by U+200D (zero-width joiner): four code points + three joiners = 7 code points, each emoji takes 2 code units, total 11. On screen it's one person, in the string it's eleven slots.

❤️ is 2, ❤ is 1. The difference is the invisible U+FE0F (variation selector), which renders a "black-and-white heart" as a "color heart." Remove it, the heart still shows, but the width may change.

é has two forms: 1 and 2. NFC is a precomposed code point; NFD is e + combining accent U+0301, two code points, one grapheme. The same character, .length gives 1 vs 2, and === comparison directly returns false.

①, ㈠, ㊙, 한, fullwidth A: all .length=1, Han hits 0. They look like characters in a Chinese context, but not one falls in the \u4e00-\u9fff range. If you pad with fullwidth letters or circled numbers, the gate counts you as 0.

The last row, "mixed", is more telling: ①𠀀👍 has .length=5, code points 3, Han hits 0, but \p{Script=Han} hits 1. Of the three characters, two are symbols that "look like Chinese context" (circled number and emoji), and the only true Han character is the rare one in the middle. If an article is padded with symbols, the four counting methods give answers from 3 to 5, and the number the gate recognizes stays 0. That's the source of my initial "I think it's long enough, but the gate says no."

Step 2: The real bug is on rare characters

𠀀 (U+20000) and 𠮷 (U+20BB7) are Han characters in CJK Extension B, .length=2 (surrogate pair), code points 1, graphemes 1, but \u4e00-\u9fff hits 0, while \p{Script=Han} hits 1.

In other words: these two are indeed Han characters, but my regex doesn't recognize a single one.

To confirm this isn't just a "missing a few characters" small thing, I ran a self-test using the gate's actual logic: construct a body with 1000 common Han characters + 200 𠀀, the true Han count is 1200.

Result:

True Han count = 1200, script Han count = 1000, gate PASS = false

An article that just meets the 1200-character threshold, if it contains 200 Extension B characters, gets judged as 1000 and is blocked at the gate. Worse, if someone pads with rare characters, the gate can't count them—the same regex errs in both directions.

Can adding ranges fix it? \u3400-\u4dbf is Extension A, can save a batch; but U+20000 is beyond the BMP, \uXXXX syntax can't express it. You need \u{20000} with the u flag, or use property escapes.

Step 3: Title truncation is a different matter

Titles have a length limit, and platforms count by .length. I measured two real titles:

The first title is 9 characters to the eye, 19 positions to the input field, the 10 difference is all joiners and surrogate pairs. If I add a clause thinking "I still have room for 10 characters," the title will be truncated mid-sentence. The second only differs by 1, a "happens once in a while" level that's easy to ignore.

I also measured punctuation: ,。;:「」()【】——…·  these 15 symbols, .length=15, Han hits 0, including a fullwidth space U+3000. They take space but aren't characters—"enough characters" and "enough code units" were never the same thing.

What I finally changed it to

const seg = new Intl.Segmenter('zh', { granularity: 'grapheme' });
const graphemes = [...seg.segment(text)].length;      // visual character count
const codeUnits = text.length;                        // platform truncation uses this
const hanzi = (text.match(/\p{Script=Han}/gu) || []).length; // Han characters (including extensions)

Three lines each do one thing; don't substitute one for another. After the change, I reran the 14 samples: 𠀀's Han count went from 0 to 1, and the gate self-test "1000+200" returned to 1200, PASS=YES.

There's an even sneakier pitfall I discovered while changing the code. \p{Script=Han} must have the u flag, but without u it doesn't throw an error: /\p{Script=Han}/.test('汉') returns false, while /\p{Script=Han}/.test('p{Script=Han}') returns true. Without u, \p is treated as an escaped literal letter p, and the whole regex degenerates into a weird thing that matches the literal p{Script=Han}. It doesn't throw, doesn't warn, just always hits 0 on real text—this is much harder to debug than a direct error, because your code "runs", and the result is empty. Adding u makes the same regex return true for 汉 and false for that literal, and behavior becomes correct.

Pitfall checklist

  1. .length is code units, not character count. Common Chinese characters are 1:1, emoji are 2 or more, ZWJ sequences can be 11.
  2. [...s].length is code point count, and also not equal to character count. 👨‍👩‍👧‍👦 counts 7, ❤️ counts 2, combining accents count 2.
  3. \u4e00-\u9fff only covers the basic block. Extension A needs an added range; Extension B and beyond only work with \u{...} plus u, or \p{Script=Han}.
  4. Title limits are based on code units. Titles with emoji hit the ceiling first; don't estimate by visual character count.
  5. The same character can have two code point forms (NFC/NFD). Normalize before storing, or deduplication, comparison, and equality checks will all go wrong.

Takeaway

Counting "how many characters" and counting "how many bytes" are two different problems: for people, use graphemes; for machine truncation, use code units; for gate decisions, use \p{Script=Han}. Mixing them up causes errors not of one or two, but on the order of "1200 judged as 1000."

Comments

Top 1 of 2 from juejin.cn, machine-translated. The original thread is authoritative.

组合搭配函数

这坑我熟,length 和「几个字」根本不是一回事,emoji 一进来就全乱套。三种计数各管各的,这话说得在点子上,混着用才得出事。

bai273

确实是一个东西三个数,而且最容易踩的是混着用:我那边的闸门按「U+4E00 到 U+9FFF 区间」数,正文里只要出现一个 U+20000 之后的字(𠀀、𠮷这种),那一个字就直接算 0,1200 字的稿子被数成 1000,PASS 变 false。所以现在分开用:要「几个字」给读者看就用 Intl.Segmenter 按字素切,👨‍👩‍👧‍👦 算 1;要卡字符上限才用码点数,算 7;.length 只当「UTF-16 单元数」,不再拿它当字数。你说得对,混着用才出事。