javascript - 在正则表达式中组合 AND 和 OR 运算符

标签 javascript regex

我想在一个正则表达式中组合一个 AND 和一个 OR 运算。

我有两套标签。集合 A 和集合 B。在集合内我需要一个 OR 操作,在集合之间需要一个 AND。

例子:

Set A has 3 values:
A1, A2, A3
Set B has 2 values:
B1 and B2

示例搜索操作:

A1 OR A2 AND B2
A2 OR A3 AND B1 OR B2
...

知道如何实现吗?

最佳答案

Within a set i need an OR operation and between Sets an AND.

您可以像这样使用前瞻:

^(?=.*?(?:A1|A2|A3))(?=.*?(?:B1|B2)).*$

描述:

(?=.*?(?:A1|A2|A3)) Positive Lookahead - Assert that the regex below can be matched
.*? matches any character (except newline)
Quantifier: Between zero and unlimited times, as few times as possible, expanding as needed
(?:A1|A2|A3) Non-capturing group
1st Alternative: (null, matches any position)
2nd Alternative: A1
A1 matches the characters A1 literally (case sensitive)
3rd Alternative: A2
A2 matches the characters A2 literally (case sensitive)
4th Alternative: A3
A3 matches the characters A3 literally (case sensitive)
(?=.*?(?:B1|B2)) Positive Lookahead - Assert that the regex below can be matched
.*? matches any character (except newline)
Quantifier: Between zero and unlimited times, as few times as possible, expanding as needed
(?:B1|B2) Non-capturing group
1st Alternative: (null, matches any position)
2nd Alternative: B1
B1 matches the characters B1 literally (case sensitive)
3rd Alternative: B2
B2 matches the characters B2 literally (case sensitive)

关于javascript - 在正则表达式中组合 AND 和 OR 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21670287/

相关文章:

c# - 用于匹配引号和单引号的正则表达式

javascript - worklight 中的 JSon 解析错误

javascript - 围绕纯数字变量值的引号

javascript - RrequireJS优化器以及优化后加载动态路径

php - htaccess 阻止访问 .php 并且只允许使用 RewriteRule

javascript - 检查URL是否包含www或https ://for RegEx

javascript - 无法捕获由 js 附加的类的单击事件

javascript - 通过javascript在弹出图像上添加下一个和上一个

c - 如何测试字符串是否遵循 C 中的给定模式?

regex - 在vim,正则表达式中清空文件中的所有方法?