Intl.Segmenter exposes boundary segmentation through the ECMAScript Internationalization API. It can segment text into extended grapheme clusters, words or sentences, depending on the selected granularity. This solves a different problem from UTF-8 decoding, character normalization or font shaping: segmentation identifies useful boundaries in the logical text string.
Construct a segmenter with a declared locale and granularity
new Intl.Segmenter('en', {granularity:'grapheme'}) requests grapheme boundaries, while 'word' and 'sentence' request different rules. The supported locale is negotiated by the implementation.
Do not use a word segmenter to determine backspace behavior or a grapheme segmenter to infer linguistic word boundaries. The granularity is a key part of an API's semantic contract; expose it explicitly when wrapping the platform API.
Grapheme segments may span several scalars
The string eฬ normally forms one extended grapheme cluster despite containing two scalars. The emoji ๐ฉโ๐ป can form one cluster from three code points. A grapheme segmentation result is a substring, not a single Unicode code point.
Do not assume one grapheme maps to one glyph: font ligatures can combine two grapheme clusters into one drawn glyph. Nor does one grapheme guarantee one UTF-16 code unit; supplementary scalars and ZWJ sequences may occupy several.
Segment indices are UTF-16 offsets
Each segment record includes an index property indicating its starting position in the input string measured in UTF-16 code units. That is compatible with many JavaScript string and DOM APIs but not the ordinal index of code points or clusters.
For example, the segment after a supplementary emoji will have an index advanced by two code units, even though only one code point has been traversed. When saving editor selections, map these offsets to the actual DOM Text nodes and avoid interpreting them as grapheme counts.
Word segmentation provides isWordLike metadata
Word granularity yields segments with isWordLike indicating whether a segment looks like a word for the implementation and locale. Whitespace and punctuation often produce segments for which this is false.
This is not a universal tokenizer for search engines, language models or programming source. It provides useful boundary defaults, possibly tailored using locale data. The same input may produce different word boundaries under different locale resources or versions.
Thai and dictionary-based segmentation need care
Languages such as Thai often lack whitespace between words. Implementations can use dictionary or statistical data to improve word segmentation. Results are environment-dependent and must be tested with representative native-language content.
A regex that splits on ASCII whitespace is not a substitute. Equally, a segmentation result should not be described as a universal semantic token boundary suitable for every natural language processing problem.
Sentence boundaries are not formatting line breaks
granularity:'sentence' identifies sentence-like spans according to implementation rules, not the line breaks chosen by a browser or the Unicode Line Breaking Algorithm UAX #14. Abbreviations, quotes and punctuation create edge cases.
A paragraph with hard newlines need not have one sentence per line. Keep paragraph structure and segmentation results separate, and document any higher-level language tailoring.
Segmentation is not Unicode normalization
NFC may convert eฬ into precomposed U+00E9 without changing its perceived grapheme segmentation. A service can return canonically equivalent but byte-distinct text, so exact round-trip integrity must be checked separately from segment counts.
Do not normalize simply to make segmentation easier; current Unicode algorithms are designed to handle both composed and decomposed sequences. Use explicit normalization only when a storage or matching contract requires it.
Stream large strings carefully
Iterating segmenter.segment(text) can avoid constructing an enormous array, though consumers may still materialize segments for indexing. Remember that indices refer to the original entire input string; slicing chunks arbitrarily can cut across cluster boundaries.
For streaming ingestion, preserve sufficient boundary context at chunk edges or use a library designed for streaming segmentation. Running a segmenter independently on each fixed-size substring can yield different boundaries near joins.
Write a boundary corpus with stable expectations
Include CRLF, decomposed accents, skin-tone emoji, ZWJ professions, regional-indicator flags, punctuation, Latin contractions, CJK and Thai text. Assert exact segments, offsets and isWordLike where the selected engine version is controlled.
When reporting cross-platform differences, capture locale and runtime's Unicode/ICU data version. Standards may permit tailoring, so a failure must be tied to the promised application behavior rather than one device's output.
Reproducible example and interpretation
This example isolates one operation on actual Unicode strings or semantic markup. It does not substitute for a full standards conformance suite, an accessibility audit, or a cross-platform interop test. Inspect each input scalar, note the API or library version, and define whether the expected output must preserve exact data or a narrower interpretation.
const s='A๐ฉ\u200D๐ปe\u0301';
const seg=new Intl.Segmenter('en',{granularity:'grapheme'});
console.log([...seg.segment(s)].map(({segment,index})=>({segment,index})));
// A starts at UTF-16 index 0, joined emoji at 1, e + accent at 6.
const words=new Intl.Segmenter('en',{granularity:'word'});
console.log([...words.segment('Hello, world!')].map(x=>[x.segment,x.isWordLike]));When a result differs, inspect the stage where it first changed: decoding, string operation, normalization, segmentation, serialization, layout or accessibility name computation. A perfectly rendered string can have different underlying scalars, and two scalarly identical strings can render differently.
Reference matrix and expected distinctions
| Scenario | Input/operation | Expected distinction |
|---|---|---|
| grapheme | e + U+0301 | one extended cluster |
| ZWJ emoji | ๐ฉโ๐ป | three scalars, one common cluster |
| index | UTF-16 units | not a grapheme ordinal |
| word | isWordLike | helps filter non-word segments |
| sentence | punctuation-aware | not synonymous with visual lines |
| locale | th/en/ja | may influence segmentation |
The observations above are interpreted under a declared Unicode or platform API version. If a task is language-sensitive, the locale and data version belong in the fixture; if it is semantic, accessible names and task comprehension require evidence beyond a Unicode code-point list.
Verification strategy and diagnostic controls
- grapheme. Preserve a fixture for
e + U+0301and record its exact Unicode scalar values, environment settings and expected result. Interpretation: one extended cluster. Compare actual output by the metric relevant to this contract rather than by screenshot alone. - ZWJ emoji. Preserve a fixture for
๐ฉโ๐ปand record its exact Unicode scalar values, environment settings and expected result. Interpretation: three scalars, one common cluster. Compare actual output by the metric relevant to this contract rather than by screenshot alone. - index. Preserve a fixture for
UTF-16 unitsand record its exact Unicode scalar values, environment settings and expected result. Interpretation: not a grapheme ordinal. Compare actual output by the metric relevant to this contract rather than by screenshot alone. - word. Preserve a fixture for
isWordLikeand record its exact Unicode scalar values, environment settings and expected result. Interpretation: helps filter non-word segments. Compare actual output by the metric relevant to this contract rather than by screenshot alone. - sentence. Preserve a fixture for
punctuation-awareand record its exact Unicode scalar values, environment settings and expected result. Interpretation: not synonymous with visual lines. Compare actual output by the metric relevant to this contract rather than by screenshot alone. - locale. Preserve a fixture for
th/en/jaand record its exact Unicode scalar values, environment settings and expected result. Interpretation: may influence segmentation. Compare actual output by the metric relevant to this contract rather than by screenshot alone.
Include both ordinary input and edge cases such as decomposed combining accents, UTF-16 surrogate pairs, emoji ZWJ sequences and bidirectional text where relevant. Store exact fixtures and expected outputs so later updates to Unicode, ICU and browser engines can be compared reliably.
How to interpret differences
Do not equate visually similar text with character identity or conflate character count with byte size. When a system deliberately transforms or presents data differently, record that as a product policy rather than silently applying it to all strings. Primary-source algorithms and application-level choices serve different purposes.
Primary standards and technical documentation
Consult the following standards and API documentation to verify feature support, normative wording and known limitations. Unicode data and browser behavior change between releases, while the user's source text should remain available for independent inspection.
- ECMA-402 Intl.Segmenter โ primary standard or technical data reference. Consult the applicable revision for normative terms, version-dependent behavior and the defined scope of interoperability claims.
- MDN Intl.Segmenter โ primary standard or technical data reference. Consult the applicable revision for normative terms, version-dependent behavior and the defined scope of interoperability claims.
- UAX #29 Unicode Text Segmentation โ primary standard or technical data reference. Consult the applicable revision for normative terms, version-dependent behavior and the defined scope of interoperability claims.
- ICU Boundary Analysis โ primary standard or technical data reference. Consult the applicable revision for normative terms, version-dependent behavior and the defined scope of interoperability claims.
- UAX #14 Unicode Line Breaking โ primary standard or technical data reference. Consult the applicable revision for normative terms, version-dependent behavior and the defined scope of interoperability claims.
Related published research
This is part of the Text APIs & Implementation Patterns virtual silo. The following already published articles provide detailed context on neighboring Unicode text-processing and interoperability operations.