CKTIS / JavaScript
Intl.Segmenter: segment() and isWordLike
Split text into locale-sensitive segments and use `isWordLike` to distinguish segments treated as words from the other segments in the result.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
const segmenter = new Intl.Segmenter(locales, { granularity: "word" });
const segments = segmenter.segment(input);
for (const { segment, index, input: source, isWordLike } of segments) {
// Use the segment data here.
}Intl.Segmenter.prototype.segment and isWordLike serve different roles: segment(input) is an instance method, while isWordLike is a property on individual segment records in word mode. The method returns a new iterable Segments object, not an array. Boundaries use the resolved locale and the granularity chosen in the constructor: "grapheme" (the default, for grapheme clusters), "word", or "sentence". The method converts its argument to a string. A Symbol argument or an invalid this receiver causes a TypeError.
Each record yielded by iteration contains segment (the substring), index (its zero-based start in UTF-16 code units), and input (the entire converted string). The exclusive end is index + segment.length. The index is not a count of visible characters.
Only in "word" mode does each record have a Boolean isWordLike property: true means the segment is treated as word-like, and false means it isn't. Spaces and punctuation generally remain in the result with false, so filter the records if you want only word-like segments. In the other modes, the property is absent rather than set to false.
The ECMAScript 2024 edition of ECMA-402 specifies this API, but runtime support varies. Both boundaries and classification can vary by locale and implementation. Don't assume identical results across runtimes.
Examples
const segmenter = new Intl.Segmenter("en", { granularity: "word" });
const words = [];
for (const { segment, index, isWordLike } of segmenter.segment("Hi, Sam!")) {
console.log(JSON.stringify([index, segment, isWordLike]));
if (isWordLike) words.push(segment);
}
console.log(JSON.stringify(words));Result
[0,"Hi",true] [2,",",false] [3," ",false] [4,"Sam",true] [7,"!",false] ["Hi","Sam"]
The first five lines show the index, substring, and classification, including the space and punctuation. The last line contains only the segments marked `true`. This is the expected output for this English text, not a guarantee across runtimes.
Related lessons
- JavaScript · Lesson 24 · Advanced · Version: 4
Sources
- ECMAScript® 2024 Internationalization API Specification — Intl.Segmenter.prototype.segment
- ECMAScript® 2024 Internationalization API Specification — CreateSegmentDataObject
- ECMAScript Internationalization API Specification — FindBoundary
- Intl.Segmenter.prototype.segment() — MDN
- Intl.Segmenter() constructor — MDN