简体   繁体   English

javascript正则表达式只有在另一个模式不匹配时才排除模式

[英]javascript regex to exclude a pattern only if another pattern does not match

A regex is needed in order to exclude sentences with wag[.|on] or ut[i|e] but only if NO sed[.|an] is present and allow all other sentences. 需要正则表达式才能用wag[.|on]ut[i|e]排除句子,但仅当没有sed[.|an]并且允许所有其他句子时才需要。 any suggestions? 有什么建议?
ie. 即。 exclude matches that are only wagon or only ute. 排除只有货车或只有ute的匹配。

I tried /[^wag[on|.]]/ig.test(sentence) but this way will not allow exclusion. 我试过/ [ /[^wag[on|.]]/ig.test(sentence)但这种方式不允许排除。 I need to select the "yes" sentences only as below. 我需要选择“是”句子,如下所示。

Given the following sentences: 鉴于以下句子:
the sedan is fast <-- yes 轿车很快< - 是的
other sed. 其他sed。 is fast too <-- yes 很快< - 是的
the wagon is slow <-- no 旅行车很慢< - 没有
other wag. 其他的摇摆。 is also slow <-- no 也很慢< - 没有
the ute is slow <-- no ute很慢< - 没有
other uti is also slow <-- no 其他uti也很慢< - 没有
the wag. 摇摇晃晃。 and wagon slower then sed. 和马车比较慢。 or sedan <-- yes 或者轿车< - 是的
the uti or ute is slower then sed. uti或ute比sed慢。 or sedan <-- yes 或者轿车< - 是的
both wag. 两个都摇摆。 wagon and uti and ute are slow <-- no 旅行车和uti和ute很慢< - 没有
nothing is fast or slow <-- yes 没有什么是快或慢< - 是的

this does the trick 这样做的伎俩

function isMatch(input) {
   var regno = /(wag[.|on]|ut[i|e])/gi;
   var regyes = /sed[.|an]/gi;
   return !regno.test(input) || regyes.test(input);
}

result: 结果:

在此输入图像描述

/^(?:.*sed[.|an].*|(?:(?!wag[.|on]|ut[i|e]).)*)$/

您可以使用(?!)匹配与某些模式不匹配的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM