忽略包含特定单词的模式

Ignore pattern with speciefic word in it

本文关键字:模式 单词 包含特      更新时间:2023-09-26

我得到了以下代码:

var one = "What a lovely puppy! 
    Is it 
    a beagle?",
    two = "You've got a lovely funny beagle!",
    three = "Where's my lovely dog? Where's my little beagle?";
var target = (".* lovely(?!funny).* beagle.*");

我需要的是"捕捉"lovelybeagle之间的单词。如果在我的"catch"边界之间有单词funny(var twomatch()应该忽略它。

换句话说,它应该检查从lovelybeagle的所有短语是否匹配,这些短语不涉及单词funny

我的var目标似乎写得不正确。

我期望:

 one.match(target) //return: "puppy! Is it a"
 two.match(target) //must be ignored, because of word "funny"
 three.match(target) //return: "dog? Where's my little"

任何帮助都将不胜感激!

您必须加入funny.*的重复测试。尝试

lovely(?:(?!funny).)*beagle

在regex101上查看它。

问候

我建议对第一个字母进行每个字符的检查,如果当前字母是初始f:,则检查"滑稽"

var regex = /'blovely'b([^f]|'Bf|f(?!unny'b))+'bbeagle'b/;