微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Javascript正则表达式匹配句子

如何解决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 \”,并且它将匹配尽可能少的字符。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。