JavaScript/Segmentation
[i]LearnΒ· Internationalization

Segmentation

The Intl.Segmenter object is useful for breaking a string into segments. Without Intl, you are already able to split a string by UTF-16 code units and Unicode code points:

const str = "πŸ‡ΊπŸ‡ΈπŸ‡¨πŸ‡³πŸ‡·πŸ‡ΊπŸ‡¬πŸ‡§πŸ‡«πŸ‡·";
console.log(str.split(""));
// Array(20) ['\uD83C', '\uDDFA', '\uD83C', ...]
console.log([...str]);
// Array(10) ['πŸ‡Ί', 'πŸ‡Έ', 'πŸ‡¨', 'πŸ‡³', 'πŸ‡·', 'πŸ‡Ί', 'πŸ‡¬', 'πŸ‡§', 'πŸ‡«', 'πŸ‡·']

But as you can see, Unicode code points are not the same as what human users perceive as discrete characters. This often happens with emojis, where a single emoji can be represented by multiple code points. When the user interacts with text, a grapheme is the smallest unit of text they can manipulate, such as delete or select. The Segmenter object enables grapheme-level segmentation, which is useful for counting characters, measuring text width, and so on. It takes a string and returns an iterable segments object, each element of which has a segment property representing the text of the segment.

const segmenter = new Intl.Segmenter("en-US", { granularity: "grapheme" });
console.log([...segmenter.segment("πŸ‡ΊπŸ‡ΈπŸ‡¨πŸ‡³πŸ‡·πŸ‡ΊπŸ‡¬πŸ‡§πŸ‡«πŸ‡·")].map((s) => s.segment));
// ['πŸ‡ΊπŸ‡Έ', 'πŸ‡¨πŸ‡³', 'πŸ‡·πŸ‡Ί', 'πŸ‡¬πŸ‡§', 'πŸ‡«πŸ‡·']

The segmenter can also do higher-level segmentation, including word-level and sentence-level splitting. These use cases are necessarily language-specific. For example, the following is a very poor implementation of word-counting:

const wordCount = (str) => str.split(/\s+/).length;
console.log(wordCount("Hello, world!")); // 2

There are several problems with this: not all languages use spaces to separate words, not all spaces are word-separating, and not all words are separated by spaces. To solve this, use Segmenter with granularity: "word". The result is the input string, split into segments of words and non-words. If you are counting words, you should filter out the non-words by checking each segment's isWordLike property.

const segmenter = new Intl.Segmenter("en-US", { granularity: "word" });
const str = "It can even split non-space-separated words";
console.table([...segmenter.segment(str)]);
// β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
// β”‚ segment     β”‚ index β”‚ isWordLike β”‚
// β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
// β”‚ 'It'        β”‚ 0     β”‚ true       β”‚
// β”‚ ' '         β”‚ 2     β”‚ false      β”‚
// β”‚ 'can'       β”‚ 3     β”‚ true       β”‚
// β”‚ ' '         β”‚ 6     β”‚ false      β”‚
// β”‚ 'even'      β”‚ 7     β”‚ true       β”‚
// β”‚ ' '         β”‚ 11    β”‚ false      β”‚
// β”‚ 'split'     β”‚ 12    β”‚ true       β”‚
// β”‚ ' '         β”‚ 17    β”‚ false      β”‚
// β”‚ 'non'       β”‚ 18    β”‚ true       β”‚
// β”‚ '-'         β”‚ 21    β”‚ false      β”‚
// β”‚ 'space'     β”‚ 22    β”‚ true       β”‚
// β”‚ '-'         β”‚ 27    β”‚ false      β”‚
// β”‚ 'separated' β”‚ 28    β”‚ true       β”‚
// β”‚ ' '         β”‚ 37    β”‚ false      β”‚
// β”‚ 'words'     β”‚ 38    β”‚ true       β”‚
// β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

console.log(
  [...segmenter.segment(str)].filter((s) => s.isWordLike).map((s) => s.segment),
);
// ['It', 'can', 'even', 'split', 'non', 'space', 'separated', 'words']

Word segmentation works for character-based languages too. For example, in Chinese, several characters can represent a single word, but there's no space between them. The segmenter implements the same behavior as the browser's built-in word segmentation, triggered by double-clicking a word.

const segmenter = new Intl.Segmenter("zh-Hans", { granularity: "word" });
console.log([...segmenter.segment("ζˆ‘ζ˜―θΏ™η―‡ζ–‡ζ‘£ηš„δ½œθ€…")].map((s) => s.segment));
// ['ζˆ‘ζ˜―', 'θΏ™', 'η―‡', 'ζ–‡', 'ζ‘£', 'ηš„', 'δ½œθ€…']

Sentence segmentation is similarly complex. For example, in English, there are many punctuation marks that could mark the end of a sentence (".", "!", "?", and so on).

const segmenter = new Intl.Segmenter("en-US", { granularity: "sentence" });
console.log(
  [...segmenter.segment("I ate a sandwich. Then I went to bed.")].map(
    (s) => s.segment,
  ),
);
// ['I ate a sandwich. ', 'Then I went to bed.']

Note that the segmenter doesn't remove any characters. It just splits the string into segments, each of which is a sentence. You can then remove the punctuation marks if you want. Also, the current implementation of the segmenter doesn't support sentence segmentation suppressions (preventing sentence breaks after periods like "Mr." or "Approx."), but there's ongoing work to support this.

Source: Internationalization by Mozilla Contributors, licensed under CC-BY-SA-2.5. Retrieved 2026-11-18. Changes: Extracted the section "Segmentation"; removed the code-block language labels; made links absolute; examples are the page's own code blocks, unchanged.