Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Date and Intl

The Intl.Segmenter object

Reference for Intl.Segmenter: split text into user-perceived characters, words or sentences for any language, including emoji and languages without spaces.

Intl.Segmenter splits text at the boundaries a reader would see. A grapheme is one user-perceived character, which may be several code points: a family emoji, a flag or a letter with combining marks. A word boundary in Japanese or Thai cannot be found by looking for spaces. A sentence boundary is not simply a full stop.

String methods like length, slice() and split("") work on UTF-16 code units, so they cut emoji in half. A segmenter returns an iterable of pieces that respect the rules of the locale.

JavaScript
const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
[...segmenter.segment("a๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งรฉ")].length; // 3

Try it

JS Counting characters

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Words and sentences

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
new Intl.Segmenter()
new Intl.Segmenter(locales)
new Intl.Segmenter(locales, options)

segmenter.segment(text)            // a Segments object
segments[Symbol.iterator]()        // iterate with for...of or spread
segments.containing(index)
segmenter.resolvedOptions()
Name Description
locales A language tag, an array of tags, or undefined for the default locale.
options.granularity "grapheme" (default), "word" or "sentence".
text The string to segment.
index A code-unit index passed to containing().

Return value: segment() returns a lazy, iterable Segments object. Each item is { segment, index, input }, where index is the start position in code units. Word segments also have isWordLike, which is true for words and numbers and false for spaces and punctuation. containing(index) returns the item that includes that position, or undefined if the index is out of range.

Exceptions: a RangeError is thrown for an invalid locale tag or granularity.

Usage notes

  • It is lazy. Segments are produced as you iterate, so you can stop early on long text.
  • Filter with isWordLike to keep words and drop spaces and punctuation. Numbers such as 3.5 count as word-like.
  • Counting characters in a form field? Use grapheme segmentation, not length, if you want the count users see.
  • Indexes are code units, so they work with slice() and substring() on the original string.
  • Locale matters for word and sentence rules. Dictionary-based splitting makes Japanese, Chinese and Thai usable; results can differ between engines.
  • Sentence breaks are heuristic. Abbreviations and unusual punctuation may be split differently across engines, so do not use it for strict parsing.
  • Segments include trailing spaces in sentence mode; call trim() if you only want the text.
  • Reversing a string: [...segmenter.segment(text)] reversed keeps emoji sequences intact, unlike split("").reverse().

Browser support

Intl.Segmenter is available in current versions of the major browsers and in Node.js. Check a compatibility table if you must support older ones, and feature-detect with typeof Intl.Segmenter === "function".