Counting words looks like a one-liner: text.split(/\s+/).length. It works for English prose and fails quietly for most of the world. Here is what I ran into while building a word counter that runs entirely in the browser, and what I ended up doing instead.
Where split(/\s+/) breaks
Many writing systems do not put spaces between words. Take a Japanese sentence:
"今日は天気がいいですね。".split(/\s+/).length; // 1
One "word". Chinese, Thai, Lao, Khmer and Myanmar do not put spaces between words either. A counter based on whitespace is wrong for a large share of users, and nothing tells them it is wrong.
Let the platform find word boundaries
Intl.Segmenter does locale-sensitive text segmentation: you pick a granularity (graphemes, words or sentences) and iterate over the segments, and it knows that Japanese needs different rules from English:
const words = new Intl.Segmenter(undefined, { granularity: "word" });
function countWords(text) {
let count = 0;
for (const part of words.segment(text)) {
if (part.isWordLike) count += 1;
}
return count;
}
The isWordLike flag matters: the segmenter also returns spaces and punctuation as segments, and you do not want to count those. On Node 24 this gives:
[...segment("Hello, world! ... ?")] // ["Hello", "world"]
[...segment("今日は天気がいいですね。")] // ["今日","は","天気","が","いい","です","ね"]
[...segment("สวัสดีครับ ผมชื่อสมชาย")] // ["สวัสดี","ครับ","ผม","ชื่อ","สมชาย"]
(segment here stands for the word segmenter above, filtered to word-like parts.) The split is not perfect and can differ between engines and versions, so I treat the numbers as "good estimates", not as exact truths. Hyphenated words are also worth knowing about: e-mail is counted as two words, and don’t and example.com as one.
Sentences are not "split on a dot"
Splitting sentences on . miscounts decimals and domains. The sentence granularity handles them:
[...new Intl.Segmenter("en", { granularity: "sentence" }).segment("Pi is 3.14. See example.com! Done?")]
// ["Pi is 3.14. ", "See example.com! ", "Done?"]
I keep only segments that contain at least one letter or digit (/[\p{L}\p{N}]/u), so a stray ... does not become a sentence.
"Characters" has three answers
The same emoji, a family of three people, measured three ways:
const s = "👨👩👧";
s.length; // 8 (UTF-16 code units)
[...s].length; // 5 (code points, three emoji joined by two zero-width joiners)
[...new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(s)].length; // 1 (what a reader sees)
string.length counts UTF-16 code units, so anything outside the Basic Multilingual Plane counts twice. Spreading the string iterates by code point, which fixes plain emoji and is what my counter uses. If you need "what a person would call one character", use the grapheme granularity instead. Which one is right depends on the limit you are checking: a database column, an SMS and a social post each count differently.
A fallback for older browsers
MDN lists Intl.Segmenter as widely available since April 2024, but older browsers and runtimes still exist. I feature-detect it and fall back to whitespace words and punctuation-based sentences, and I test both paths:
const supported = typeof Intl !== "undefined" && "Segmenter" in Intl;
The fallback is worse for Japanese, Chinese and Thai, but it never crashes and it is right for space-separated languages.
Takeaways
- Do not count words with
split(/\s+/)unless you only ever see space-separated text. - Use
Intl.Segmenterwithgranularity: "word"and checkisWordLike. - Decide what "character" means for your use case: code units, code points or graphemes.
- Keep a fallback and test it, and expect small differences between engines.
The counter I built this for runs in the browser without sending text anywhere: https://medleyo.com/word-counter/
Top comments (0)