通过正则表达式和javascript,我只需要通过关键字“a”“b”和“c”来行(1)和(3)。
文字是:
a + b + c(1)
测试+ a +测试(2)
c + b + a(3)
test + b + test(4)
我找到了“(?=。* a)。* b”。但是,我如何处理超过2个关键字,如示例?
答案 0 :(得分:1)
您可以使用正向前瞻断言,如以下正则表达式中所示,以匹配所需的文本:
/^(?=.*a)(?=.*b)(?=.*c)/
有关 lookahead 和 lookbehind 的更多详细信息,请访问:Regex lookahead, lookbehind and atomic groups
JavaScript演示
var a = "a+b+c";
var b = "test+a+test";
var c = "c+b+a";
var d = "test+b+test";
var pattern = /^(?=.*a)(?=.*b)(?=.*c)/;
console.log(a + " ----- " + pattern.test(a));
console.log(b + " ----- " + pattern.test(b));
console.log(c + " ----- " + pattern.test(c));
console.log(d + " ----- " + pattern.test(d));