Intl.Segments.prototype.containing()

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.Segments.containing() メソッドは、指定されたインデックスのコードユニットを含む文字列中のセグメントを記述したオブジェクトを返します。

試してみましょう

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

const segments = segmenterFr.segment(string1);

console.log(segments.containing(5));
// Expected output:
// Object {segment: 'ma', index: 4, input: 'Que ma joie demeure', isWordLike: true}

構文

js
containing(codeUnitIndex);

引数

codeUnitIndex 省略可

元の入力文字列におけるコードユニットのインデックスを指定する数値。省略した場合のデフォルトは 0 となります。

返値

元の文字列のセグメントを記述するオブジェクトで以下のプロパティを持ちます。与えられたインデックス値が範囲外の場合は undefined となります。

segment

元の入力文字列から抽出されたセグメントを含む文字列。

index

セグメントを開始する元の入力文字列のコードユニットのインデックス。

input

セグメント化された完全な入力文字列。

isWordLike

granularity"word" の場合のみブール値となり、それ以外は undefined です。granularity"word" の場合、isWordLike は、セグメントが単語のようなもの(すなわち、文字/数字/英字/その他)である場合に true、それ以外の場合に false となります。

js
// ┃0 1 2 3 4 5┃6┃7┃8┃9  ← code unit index
// ┃A l l o n s┃-┃y┃!┃   ← code unit
const input = "Allons-y!";

const segmenter = new Intl.Segmenter("fr", { granularity: "word" });
const segments = segmenter.segment(input);
let current = undefined;

current = segments.containing();
// → { index: 0, segment: "Allons", isWordLike: true }

current = segments.containing(4);
// → { index: 0, segment: "Allons", isWordLike: true }

current = segments.containing(6);
// → { index: 6, segment: "-", isWordLike: false }

current = segments.containing(current.index + current.segment.length);
// → { index: 7, segment: "y", isWordLike: true }

current = segments.containing(current.index + current.segment.length);
// → { index: 8, segment: "!", isWordLike: false }

current = segments.containing(current.index + current.segment.length);
// → undefined

仕様書

Specification
ECMAScript® 2025 Internationalization API Specification
# sec-%segmentsprototype%.containing

ブラウザーの互換性

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
containing

Legend

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

Full support
Full support