简体   繁体   中英

Validate LHS+OPERATOR+RHS regular expression in JavaScript?

I have only these operators : > , >= , < , <= , == , != , 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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