admin管理员组

文章数量:1291120

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 wele (not split() necessarily) - Thanks!

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 wele (not split() necessarily) - Thanks!

Share Improve this question edited Jun 21, 2012 at 15:27 Andres SK asked Jun 20, 2012 at 19:51 Andres SKAndres SK 11k27 gold badges96 silver badges158 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 8

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/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"]

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.

what about

var sentences = $('#text').val().split(/\r\n|\r|\n|\s/gi);

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.

本文标签: jqueryIncluding separator characters in split (javascript)Stack Overflow