Created
October 13, 2026 15:24
-
-
Save scripting/5e9a9a1d79f13176f8b6b11dd3da6cbf to your computer and use it in GitHub Desktop.
Get the first sentence in a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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