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

Try it

const string1 = "Que ma joie demeure";

const segmenterFrGrapheme = new Intl.Segmenter("fr", {
  granularity: "grapheme",
});
const graphemeSegments = segmenterFrGrapheme.segment(string1);

console.log(Array.from(graphemeSegments)[0]);
/ Expected output:
/ Object {segment: 'Q', index: 0, input: 'Que ma joie demeure'}

Syntax

js
segment(input)

Parameters

input

The text to be segmented as a string.

Return value

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]: «?»

Specifications

Specification
Product help
  • Constructor
    1. Intl.Segmenter.prototype.segment()
  • Inheritance
  • Function.prototype.call()
  • Function: length
  • Function.prototype.caller\nNon-standard\n\nDeprecated\n
  • Instance methods
    1. Object.prototype.__lookupSetter__()\nDeprecated\n
    2. Object.prototype.toLocaleString()
    3. Object.prototype.constructor
  • Related pages
  • Intl.DisplayNames
  • Intl.NumberFormat
  • Intl.RelativeTimeFormat
  • ","source":{"folder":"en-us/web/javascript/reference/global_objects/intl/segmenter/segment","github_url":"https://github.com/mdn/content/blob/main/files/en-us/web/javascript/reference/global_objects/intl/segmenter/segment/index.md","last_commit_url":"https://github.com/mdn/content/commit/9645d14f12d9b93da98daaf25a443bb6cac3f2a6","filename":"index.md"},"summary":"The segment() method of Intl.Segmenter instances segments a string according to the locale and granularity of this Intl.Segmenter object.","title":"Intl.Segmenter.prototype.segment()","toc":[{"text":"Try it","id":"try_it"},{"text":"Syntax","id":"syntax"},{"text":"Examples","id":"examples"},{"text":"Specifications","id":"specifications"},{"text":"Browser compatibility","id":"browser_compatibility"}],"baseline":{"baseline":"low","baseline_low_date":"2024-04-16","support":{"chrome":"87","chrome_android":"87","edge":"87","firefox":"125","firefox_android":"125","safari":"14.1","safari_ios":"14.5"},"feature":{"status":{"baseline":"low","baseline_low_date":"2024-04-16","support":{"chrome":"87","chrome_android":"87","edge":"87","firefox":"125","firefox_android":"125","safari":"14.1","safari_ios":"14.5"}},"description_html":"The Intl.Segmenter API creates a locale-aware text splitter that can separate a string into meaningful graphemes, words, or sentences.","name":"Intl.Segmenter"}},"browserCompat":["javascript.builtins.Intl.Segmenter.segment"],"pageType":"javascript-instance-method"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant