Intl.Segmenter

Baseline 2024
Newly available

Since April 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Intl.Segmenter オブジェクトは、ロケールに応じたテキストのセグメンテーションを可能にし、文字列から意味のある項目(書記素、単語、文)を取得することができます。

試してみましょう

コンストラクター

Intl.Segmenter()

新しい Intl.Segmenter オブジェクトを作成します。

静的メソッド

Intl.Segmenter.supportedLocalesOf()

指定したロケールのうち、ランタイムのデフォルトロケールにフォールバックすることなくサポートされているものを含む配列を返します。

インスタンスメソッド

Intl.Segmenter.prototype.resolvedOptions()

この Intl.Segmenter オブジェクトの初期化時に計算されたロケールおよび粒度のオプションを反映したプロパティを持つ新しいオブジェクトを返します。

Intl.Segmenter.prototype.segment()

この Intl.Segmenter のインスタンスのロケールおよび粒度に従って文字列のセグメントを表す、新しい反復可能な Segments のインスタンスを返します。

基本的な使い方と String.prototype.split() との相違点

String.prototype.split(" ") を使ってテキストを単語に分割する場合、テキストのロケールが単語間の空白を使用しない場合(つまり、日本語、中国語、タイ語、ラオス語、クメール語、ミャンマー語などの場合)、正しい結果を得ることはできません。

js
const str = "吾輩は猫である。名前はたぬき。";
console.table(str.split(" "));
// ['吾輩は猫である。名前はたぬき。']
// この 2 文をきちんと分割できていません。
js
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.
// ]

仕様書

Specification
ECMAScript® 2025 Internationalization API Specification
# segmenter-objects

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
Segmenter
Segmenter() constructor
resolvedOptions
segment
supportedLocalesOf

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support