Intl.Segments.prototype.containing()
The Intl.Segments.containing()
method returns an object describing the segment in the string that includes the code unit at the specified index.
Syntax
containing(codeUnitIndex)
Parameters
codeUnitIndex
Optional-
A number specifying the index of the code unit in the original input string. If the value is omitted, it defaults to
0
.
Return Value
An object describing the segment of the original string with the following properties, or undefined
if the supplied index value is out of bounds.
segment
-
A string containing the segment extracted from the original input string.
index
-
The code unit index in the original input string at which the segment begins.
input
-
The complete input string that was segmented.
isWordLike
-
A boolean value only if
granularity
is"word"
; otherwise,undefined
. Ifgranularity
is"word"
, thenisWordLike
istrue
when the segment is word-like (i.e., consists of letters/numbers/ideographs/etc.); otherwise,false
.
Examples
// ā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
Specifications
Specification |
---|
ECMAScript Internationalization API Specification # sec-%segmentsprototype%.containing |
Browser compatibility
BCD tables only load in the browser