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:
Any ideas? Any approach is welcome (not split() necessarily) - Thanks!
Use .match instead (docs). When you use it with a /.../g-type regex, it returns an array of all matches. You just need to modify your regex first:
var sentences = $('#text').val().match(/[^\r\n.!?]+(\r\n|\r|\n|[.!?])\s*/gi);
http://jsfiddle.net/kEHhA/3/
var re = /[^\r\n.!?]+(:?(:?\r\n|[\r\n]|[.!?])+|$)/gi;
("First sentence.. Second sentence?? Third sentence!!\n"+ "Fourth sentence").match(re).map($.trim)
//["First sentence..", "Second sentence??", "Third sentence!!", "Fourth sentence"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With