Intl.Segmenter.prototype.segment()

Intl.Segmenter 实例的 segment() 方法根据该 Intl.Segmenter 对象的区域设置和粒度对字符串进行分段。

¥The segment() method of Intl.Segmenter instances segments a string according to the locale and granularity of this Intl.Segmenter object.

Try it

语法

¥Syntax

js
segment(input)

参数

¥Parameters

input

要分割为字符串的文本。

返回值

¥Return value

一个新的可迭代 Segments 对象,包含输入字符串的分段,使用分段器的区域设置和粒度。

¥A new iterable Segments object containing the segments of the input string, using the segmenter's locale and granularity.

示例

¥Examples

js
// Create a locale-specific word segmenter
const segmenter = new Intl.Segmenter("fr", { granularity: "word" });

// Use it to get an iterator over the segments of a string
const input = "Moi ? N'est-ce pas ?";
const segments = segmenter.segment(input);

// Use that for segmentation
for (const { segment, index, isWordLike } of segments) {
  console.log(
    "segment at code units [%d, %d]: «%s»%s",
    index,
    index + segment.length,
    segment,
    isWordLike ? " (word-like)" : "",
  );
}
// segment at code units [0, 3]: «Moi» (word-like)
// segment at code units [3, 4]: « »
// segment at code units [4, 5]: «?»
// segment at code units [5, 6]: « »
// segment at code units [6, 11]: «N'est» (word-like)
// segment at code units [11, 12]: «-»
// segment at code units [12, 14]: «ce» (word-like)
// segment at code units [14, 15]: « »
// segment at code units [15, 18]: «pas» (word-like)
// segment at code units [18, 19]: « »
// segment at code units [19, 20]: «?»

规范

Specification
ECMAScript Internationalization API Specification
# sec-intl.segmenter.prototype.segment

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility