只选择javascript中的句子

时间:2016-02-06 19:26:10

标签: javascript regex

我一直在尝试使用正则表达式来抓取以下段落中的句子:

str="Hello, my name is Mr. Bob.  How are you? I am in the F.B.I. My favorite number is 2.5."   

var res = str.match( /[^\.!\?]+[\.!\?]+/g ); 

console.log(res);

结果如下:

["Hello, my name is Lance.", "  How are you?", " I am in the F.", "B.", "I.", " My favorite number is 2.", "5."]

我如何捕捉" F.B.I。"和" 2.5"作为一个句子中的单词而不是一堆单独的句子?

1 个答案:

答案 0 :(得分:0)

空格符号或字符串的结尾可能是一个很好的提示,它是句子的结尾。

str="Hello, my name is Mr. Bob.  How are you? I am in the F.B.I. My favorite number is 2.5."   
console.log(str.match( /.*?[\.!\?]+(?:\s|$)/g ));

哪个输出:

["Hello, my name is Mr. ", "Bob. ", " How are you? ", "I am in the F.B.I. ", "My favorite number is 2.5."]

Bob除非明确指定一些通常后跟一个点的单词(如Mr,Ms,Mrs等),否则无法捕获Bob,正如@ndn在评论中提到的那样,可以找到更详细的研究{{ 3}}