This is a textarea. The user can write anything.
<textarea id="text">First sentence. Second sentence? Third sentence!
Fourth sentence.
Fifth sentence
</textarea>
At the end, i have to split all the text into an array.
var sentences = $('#text').val().split(/\r\n|\r|\n|[.|!|?]\s/gi);
The issue i’m having, is that the separator characters are not present in the array item values. This is what sentences is returning:
["First sentence", "Second sentence", "Third sentence", "Fourth sentence", "Fifth sentence"]
It should be:
["First sentence.", "Second sentence?", "Third sentence!", "", "Fourth sentence.", "", "", "Fifth sentence"]
Extra considerations:
- last sentence doesn’t require a separator character (it can end at any char)
- if a sentence has more than one separator char, it should also be included in the array item. Example: second sentence?? should be […,”second sentence??”,…]
Any ideas? Any approach is welcome (not split() necessarily) – Thanks!
1 Answer