我一直在尝试使用 Javascript 的 RegEx 来解析给定段落中的每个问题。但是,我得到了不需要的结果:
Javascript
regex = /(\S.+?[.!?])(?=\s+|$)/g;
result = regex.exec("I can see you. Where are you? I am here! How did you get there?");
预期结果
["Where are you?", "How did you get there?"]
实际结果
["I can see you.", "I can see you."]
PS:如果有更好的方法,我会洗耳恭听!
最佳答案
试试这个:
var x = string.match(/\(?[A-Z][^\.!\?]+[!\.\?]\)?/g);
x.filter(function(sentence) {
return sentence.indexOf('?') >= 0;
})
关于javascript - 捕获段落中的所有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14223893/