开发者

Regex match one of two words

开发者 https://www.devze.com 2023-03-23 20:06 出处:网络
I have an input that can have only 2 values apple or banana. What regular expression can I use to ensure that either of the 开发者_StackOverflow中文版two words was submitted?This will do:

I have an input that can have only 2 values apple or banana. What regular expression can I use to ensure that either of the 开发者_StackOverflow中文版two words was submitted?


This will do:

/^(apple|banana)$/

to exclude from captured strings (e.g. $1,$2):

(?:apple|banana)

Or, if you use a standalone pattern:

apple|banana


There are different regex engines but I think most of them will work with this:

apple|banana
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号