0

我有一个正则表达式将一个段落分成句子:

var sentences = /[^\.!\?]+[\.!\?]+/g;

[\.!\?]+如果标点符号 ( ) 后面有空格,我希望它只匹配\s。我试过/[^\.!\?]+[\.!\?]+\s/g了,但没有奏效。

我想要这个的原因是因为目前如果中间有一个带有标点符号的单词(如about.me),它会在那里拆分它,就像它没有.代表句子的结尾一样。有任何想法吗?

例如:

如果我有这一段:

If the problem being solved isn't as apparent or immediately useful as traffic about.me and navigation data: weather. A few apps are trying to harness the crowd to provide accurate?

我希望它只分裂成

['If the problem being solved isn't as apparent or immediately useful as traffic about.me and navigation data: weather.', 'A few apps are trying to harness the crowd to provide accurate?']

而目前它分为

['If the problem being solved isn't as apparent or immediately useful as traffic about.', 'me and navigation data: weather.', 'A few apps are trying to harness the crowd to provide accurate?'].

4

2 回答 2

2

这是你想要的吗?

var str = "If the problem being solved isn't as apparent or immediately useful as traffic about.me and navigation data: weather. A few apps are trying to harness the crowd to provide accurate?";

str.match(/.+?(\.|\?)(\s|$)/g);
于 2013-06-19T19:55:12.893 回答
1

使用前瞻

var re = /[\.!\?]+(?=\s)/g;

var result = "If the problem being solved isn't as apparent or immediately useful as traffic about.me and navigation data: weather. A few apps are trying to harness the crowd to provide accurate?".split(re);

console.log(result.length);  // => 2
于 2013-06-19T19:50:19.983 回答