这是一个文本区域。用户可以写任何东西。
<textarea id="text">First sentence. Second sentence? Third sentence!
Fourth sentence.
Fifth sentence
</textarea>
最后,我必须将所有文本拆分为一个数组。
var sentences = $('#text').val().split(/\r\n|\r|\n|[.|!|?]\s/gi);
我遇到的问题是数组项值中不存在分隔符。这是句子返回的内容:
["First sentence", "Second sentence", "Third sentence", "Fourth sentence", "Fifth sentence"]
它应该是:
["First sentence.", "Second sentence?", "Third sentence!", "", "Fourth sentence.", "", "", "Fifth sentence"]
额外注意事项:
- 最后一句不需要分隔符(它可以以任何字符结尾)
- 如果一个句子有多个分隔符,它也应该包含在数组项中。例:第二句??应该是 [...,"第二句??",...]
有任何想法吗?欢迎任何方法(不一定是 split() ) -谢谢!