在JavaScript中验证LHS + OPERATOR + RHS正则表达式?

时间:2016-09-15 13:27:15

标签: javascript regex string validation

我只有这些运营商:>>=<<===!=matches

我有一些像下面这样的值和预期的结果

<1.1> == 10                 Match
<1.1> != 10                 Match
<1.1> matches test          Match
<1.1> matches test|user     Match
<1.1> matches 10            Match
<1.1> matches 10|20         Match

<1.1> >= 10|20              Don't Match
<1.1> == 10|20              Don't Match
<1.1> != test               Don't Match
<1.1> == test               Don't Match

我试过这个

/^[a-zA-Z0-9_.<>]+[\s](<|<=|>|>=|==|!=|matches)[\s][a-zA-Z0-9_.<>|]+$/ 

但没有工作。

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

/^[a-zA-Z0-9_.<>]+\s(?:(?:<|<=|>|>=|==|!=)\s\d+|matches\s[a-zA-Z0-9_.<>|]+)$/gm

我已经添加了一个替代,以便将匹配与其他运营商分开。您可以看到演示here

相关问题