我有以下代码:

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之间“捕获”单词。如果在我的“捕获”边界之间有一个词“ funny”(var two),则该方法
match()应该忽略它。

换句话说,它应该检查是否匹配从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


Check it out here at regex101.

问候

10-06 00:58