Skip to content

Instantly share code, notes, and snippets.

@Vigowebs
Created December 4, 2022 04:32
Show Gist options
  • Save Vigowebs/6257541066c251c2681a07ff6c699e77 to your computer and use it in GitHub Desktop.
Save Vigowebs/6257541066c251c2681a07ff6c699e77 to your computer and use it in GitHub Desktop.
The Intl.Segmenter object enables locale-sensitive text segmentation, enabling you to get meaningful items (graphemes, words or sentences) from a string. Currently Intl.Segmenter is not supported in Firefox and Firefox for Android browsers.
const segmenterEn = new Intl.Segmenter('en', { granularity: 'word' });
const string1 = 'Split JavaScript Strings!';
[...segmenterEn.segment(string1)]
// [
// {segment: 'Split', index: 0, input: 'Split JavaScript Strings!', isWordLike: true}
// {segment: ' ', index: 5, input: 'Split JavaScript Strings!', isWordLike: false}
// {segment: 'JavaScript', index: 6, input: 'Split JavaScript Strings!', isWordLike: true}
// ...
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment