Skip to content

leadingSentence

Extracts the leading sentence from a string. null and undefined are passed through unchanged.

A sentence boundary is detected at the first occurrence of ., ?, !, …, or ; followed by whitespace or end of string. Newlines are collapsed to spaces before matching.

If no boundary is found the entire (cleaned) string is returned.

To cap the result at a maximum length, combine with truncate:

truncate(leadingSentence(input), 120)

Available since v2.0.0

import { leadingSentence } from '@helpers4/string';
leadingSentence(input: string): string
ParameterTypeDescription
inputstringThe source string

string — The first sentence, including its terminal character; or null/undefined if input is nullish

Returns the first sentence, terminated by . ? or !.

leadingSentence('Returns the sum of an array. Works with any numbers.')
// => 'Returns the sum of an array.'

Recognises question marks and exclamation marks as sentence terminators.

leadingSentence('Is it done? Yes it is!')
// => 'Is it done?'

Use truncate to limit the result to a fixed number of characters.

truncate(leadingSentence(input), 120)

View source on GitHub