gpt4 book ai didi

javascript - 正则表达式根据多个单词的存在来选择句子

转载 作者:行者123 更新时间:2023-11-28 18:08:42 25 4
gpt4 key购买 nike

我可以使用此正则表达式从单个单词中选择一个句子:

[^.]* word [^.]*\.

如何以条件方式根据两个或多个单词选择一个句子?

例如,如果存在单词 A 和(单词 B 或单词 C),则选择句子。

假设单词A =“研究”,单词B =“建议”,单词C =“证据”,则以下句子将被选中,因为有 2 个单词匹配:

Our studies suggest that ACTN3 R577X genotype is a modifier of clinical phenotype in DMD patients.

但是下面这句话,因为它只匹配了一个单词,所以不会被选中:

Moreover, studies of the RR genotype have shown a downward linear trend with increased incidences of ankle sprain.

最佳答案

我会使用:

\bworda\b[^.!?]*\b(?:wordb|wordc)\b

如果句子包含 worda wordb wordc,则将匹配该句子> 如果没有标点符号分隔。

如果您想以任意顺序匹配单词:

^(?=[^.!?]*\bstudies\b)(?=[^.!?]*\b(?:suggest|evidence)\b)[^.!?]*\.$

var test = [
'Our studies suggest that ACTN3 R577X genotype is a modifier of clinical phenotype in DMD patients.',
'Moreover, studies of the RR genotype have shown a downward linear trend with increased incidences of ankle sprain.',
'Moreover, studies of the evidence of ankle sprain.',
'Moreover, evidence of the studies of ankle sprain.',
'Moreover, the incidences of ankle sprain.'
];
console.log(test.map(function (a) {
return a+' :'+/^(?=[^.!?]*\bstudies\b)(?=[^.!?]*\b(?:suggest|evidence)\b)[^.!?]*\.$/i.test(a);
}));

关于javascript - 正则表达式根据多个单词的存在来选择句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42090276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com