问题
我正在尝试构建一个正则表达式,该表达式只在某个关键字出现在另一个关键字之后时才匹配该关键字。
在下面的文本块中,如果Foo Client
在任何点都跟在Inactive Services
后面,我只想匹配。有时Foo Client
可能在Active Services
之下,在这种情况下,我确实NOT希望它匹配。
谢谢!
示例文本:
Plugin Output:
Active Services :
Font Service [ FontService ]
Windows Driver Foundation - User-mode Driver Framework [ wudfsvc ]
Inactive Services :
Adobe Flash Player Update Service [ AdobeFlashPlayerUpdateSvc ]
Foo Client [ Foo Client ]
App Readiness [ AppReadiness ]
Inactive Services :[wW]*Foo Client
如果我能正确理解你的问题,应该会起作用。我们只需找到单词"Inactive Services",然后匹配任何字符,直到找到Foo Client。我们使用[wW]*
而不是.*,因为点与换行符不匹配。
const pos = `Plugin Output:
Active Services :
Font Service [ FontService ]
Windows Driver Foundation - User-mode Driver Framework [ wudfsvc ]
Inactive Services :
Adobe Flash Player Update Service [ AdobeFlashPlayerUpdateSvc ]
Foo Client [ Foo Client ]
App Readiness [ AppReadiness ]`;
const neg = `Plugin Output:
Active Services :
Font Service [ FontService ]
Foo Client [ Foo Client ]
Windows Driver Foundation - User-mode Driver Framework [ wudfsvc ]
Inactive Services :
Adobe Flash Player Update Service [ AdobeFlashPlayerUpdateSvc ]
App Readiness [ AppReadiness ]`;
console.log(/Inactive Services :[wW]*Foo Client/.test(pos));
console.log(/Inactive Services :[wW]*Foo Client/.test(neg));