Intl.Segmenter
Baseline Newly available
This feature works across the latest devices and browser versions. This feature might not work in older devices or browser versions. It's been available across browsers since April 2024.
The Intl.Segmenter object enables locale-sensitive text segmentation, enabling you to get meaningful items (graphemes, words or sentences) from a string.
Try it
const segmenterFr = new Intl.Segmenter("fr", { granularity: "word" });
const string = "Que ma joie demeure";
const iterator = segmenterFr.segment(string)[Symbol.iterator]();
console.log(iterator.next().value.segment);
// Expected output: 'Que'
console.log(iterator.next().value.segment);
// Expected output: ' '
Constructor
-
Intl.Segmenter() - Creates a new
Intl.Segmenterobject.
Static methods
-
Intl.Segmenter.supportedLocalesOf() - Returns an array containing those of the provided locales that are supported without having to fall back to the runtime's default locale.
Instance properties
These properties are defined on Intl.Segmenter.prototype and shared by all Intl.Segmenter instances.
-
Intl.Segmenter.prototype.constructor - The constructor function that created the instance object. For
Intl.Segmenterinstances, the initial value is theIntl.Segmenterconstructor. -
Intl.Segmenter.prototype[Symbol.toStringTag] - The initial value of the
[Symbol.toStringTag]property is the string"Intl.Segmenter". This property is used inObject.prototype.toString().
Instance methods
-
Intl.Segmenter.prototype.resolvedOptions() - Returns a new object with properties reflecting the locale and granularity options computed during initialization of this
Intl.Segmenterobject. -
Intl.Segmenter.prototype.segment() - Returns a new iterable
Segmentsinstance representing the segments of a string according to the locale and granularity of thisIntl.Segmenterinstance.
Examples
Basic usage and difference from String.prototype.split()
If we were to use String.prototype.split(" ") to segment a text in words, we would not get the correct result if the locale of the text does not use whitespaces between words (which is the case for Japanese, Chinese, Thai, Lao, Khmer, Myanmar, etc.).
const str = "吾輩は猫である。名前はたぬき。";
console.table(str.split(" "));
// ['吾輩は猫である。名前はたぬき。']
// The two sentences are not correctly segmented.
const str = "吾輩は猫である。名前はたぬき。";
const segmenterJa = new Intl.Segmenter("ja-JP", { granularity: "word" });
const segments = segmenterJa.segment(str);
console.table(Array.from(segments));
// [{segment: '吾輩', index: 0, input: '吾輩は猫である。名前はたぬき。', isWordLike: true},
// etc.
// ]
Specifications
Browser compatibility
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on iOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
Segmenter |
87 |
87 |
125 |
73 |
14.1 |
87 |
125 |
62 |
14.5 |
14.0 |
87 |
14.5 |
1.0.0 |
1.8 |
16.0.0 |
Segmenter |
87 |
87 |
125 |
73 |
14.1 |
87 |
125 |
62 |
14.5 |
14.0 |
87 |
14.5 |
1.0.0 |
1.8 |
16.0.0 |
resolvedOptions |
87 |
87 |
125 |
73 |
14.1 |
87 |
125 |
62 |
14.5 |
14.0 |
87 |
14.5 |
1.0.0 |
1.8 |
16.0.0 |
segment |
87 |
87 |
125 |
73 |
14.1 |
87 |
125 |
62 |
14.5 |
14.0 |
87 |
14.5 |
1.0.0 |
1.8 |
16.0.0 |
supportedLocalesOf |
87 |
87 |
125 |
73 |
14.1 |
87 |
125 |
62 |
14.5 |
14.0 |
87 |
14.5 |
1.0.0 |
1.8 |
16.0.0 |
See also
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter