Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including separator characters in split (javascript)

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!

like image 554
Andres SK Avatar asked Dec 19 '25 11:12

Andres SK


2 Answers

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/

like image 133
Blazemonger Avatar answered Dec 22 '25 00:12

Blazemonger


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"]
like image 31
Esailija Avatar answered Dec 22 '25 00:12

Esailija



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!