简体   繁体   English

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

[英]Validate LHS+OPERATOR+RHS regular expression in JavaScript?

I have only these operators : > , >= , < , <= , == , != , matches我只有这些运算符: >>=<<===!=matches

I have some values like below and expected Result我有一些像下面这样的值和预期的结果

<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

I tried this我试过这个

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

but not working.但不工作。

You can use the following:您可以使用以下内容:

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

I've added an alternation to separate matches from the others operators.我添加了一个选项来将匹配项与其他操作符分开。 You can see a demo here你可以在这里看到一个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM