Including Separator Characters In Split (javascript)
Solution 1:
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);
Solution 2:
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"]
Solution 3:
Does this work for your purposes? It looks like you're already using jQuery but if not it should be easy to modify:
var sentences = [];
$.each($('#text').val().split(/([^\.\?\!\r\n]+.)\s/gi), function(i, sentence) {
if(i%2 !== 0) {
sentences.push(sentence)
}
});
// sentences = ["First sentence.", "Second sentence?", "Third sentence!", "Fourth sentence."]
Edit: Blazemonger's solution is similar but more elegant, using match() instead of split() and therefore not needing the second step of removing the odd elements in the array.
Solution 4:
what about
var sentences = $('#text').val().split(/\r\n|\r|\n|\s/gi);
Solution 5:
It would be easy with look-behinds, but since JavaScript does not support it, my suggestion would be:
Find the white space characters you want to split on and replace them with some dummy character. Then split on that character.
Something like:
$('#text').val().replace(/\r\n|\r|\n|([.!?])\s/gi, '$1\0').split(/\0/g);
Edit: Apparently there are better solutions which don't rely on split. I will leave this as alternative however.
Post a Comment for "Including Separator Characters In Split (javascript)"