There are already a couple of similar questions:
- Splitting textarea sentences into array and finding out which sentence changed on keyup()
- JS RegEx to split text into sentences
- Javascript RegExp for splitting text into sentences and keeping the delimiter
- Split string into sentences in javascript
My situation is a bit different.
I need to count the number of sentences in a string.
The closest answer to what I need would be:
str.replace(/([.?!])\s*(?=[A-Z])/g, "$1|").split("|")
The only problem here is that this RegEx assumes a sentence starts with a capital letter, which may not always be the case.
To be more specific, I would define a sentence as:
- Starting with a letter (capital or not), a number or even a symbol (such as $ or €).
- Ending with a punctuation sign, such as a " . ", a " ? " or a " ! ".
However, if a sentence contains a number, which itself contains a " . " or a " , ", then the sentence should be considered as one sentence and not two.
Last but not least, we can assume that, except the first sentence, a sentence is preceded by a space.
Given a random string, how can I count the number of sentences it contains with Javascript (or CoffeeScript for that matter)?