369
votes

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 two words was submitted?

2
In what language/environment is this regex being implemented? Regex seems like unnecessary overhead in many situations.mickmackusa

2 Answers

580
votes

This will do:

/^(apple|banana)$/

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

(?:apple|banana)
114
votes

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

apple|banana