Skip to content

Instantly share code, notes, and snippets.

@scripting
Created October 13, 2026 15:24
Show Gist options
  • Select an option

  • Save scripting/5e9a9a1d79f13176f8b6b11dd3da6cbf to your computer and use it in GitHub Desktop.

Select an option

Save scripting/5e9a9a1d79f13176f8b6b11dd3da6cbf to your computer and use it in GitHub Desktop.
Get the first sentence in a string
function firstSentence (s, maxCt) {
var segmenter = new Intl.Segmenter (undefined, {granularity: "sentence"});
var iterator = segmenter.segment (s) [Symbol.iterator] ();
var item = iterator.next ();
var result = "";
if (!item.done) {
result = item.value.segment.trim ();
if (result.length > maxCt) {
result = result.substr (0, maxCt - 3);
var ix = result.lastIndexOf (" ");
if (ix > 0) {
result = result.substr (0, ix);
}
result += "...";
}
}
return (result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment