Javascript正则表达式匹配句子

| 我有以下示例段落: 以下是我的文字。我的文字介绍行是this,that和other。我的第二行与以前非常相似,但完全不同。甚至不要谈论我的第三行文字。 我想使用正则表达式捕获以下句子: 我的文字介绍行是this,that和other。 因此,我得到的代码是: (\\ b我\\简介\\ sline \\ sof \\ stext)。*(\\。) 但这得到了所有的内容。我将如何捕获直到仅第一个句点?
已邀请:
注意区别:
(\\bMy\\sintroductory\\sline\\sof\\stext)[^\\.]*\\.
只是出于非常好奇,这里是一些我的方法和Piskvor的基准测试代码。 字符类方法:通过Firefox在我的计算机上大约550ms。
var start = (new Date()).getTime();
for(var i=0;i<100000;i++){
\"The following is my text. My introductory line of text is the this, that and the other. My second line is much the same as before but completely different. Don\'t even talk about my third line of text.\".match(/(\\bMy\\sintroductory\\sline\\sof\\stext)[^\\.]*\\./);
}
var stop = (new Date()).getTime();
alert(stop - start);
非贪婪方法:通过Firefox在我的计算机上大约650毫秒。
var start = (new Date()).getTime();
for(var i=0;i<100000;i++){
\"The following is my text. My introductory line of text is the this, that and the other. My second line is much the same as before but completely different. Don\'t even talk about my third line of text.\".match(/(\\bMy\\sintroductory\\sline\\sof\\stext).*?\\./);
}
var stop = (new Date()).getTime();
alert(stop - start);
如果可以的话,请留下您的评论。谢谢! 请不要发表有关微优化的意见。我只是好奇 ;)。
(\\bMy\\sintroductory\\sline\\sof\\stext).*?\\.
这样就形成了
*
\“ ungreedy \”,并且它将匹配尽可能少的字符。

要回复问题请先登录注册