このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

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

試してみましょう

const segmenterFr = new Intl.Segmenter("fr", { granularity: "word" });
const string1 = "Que ma joie demeure";

const iterator1 = segmenterFr.segment(string1)[Symbol.iterator]();

console.log(iterator1.next().value.segment);
// Expected output: 'Que'

console.log(iterator1.next().value.segment);
// Expected output: ' '

コンストラクター

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® 2026 Internationalization API Specification
# segmenter-objects

ブラウザーの互換性