490

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?

CyberJunkie
  • 21,596
  • 59
  • 148
  • 215
  • 4
    In what language/environment is this regex being implemented? Regex seems like unnecessary overhead in many situations. – mickmackusa Jun 01 '19 at 09:31

2 Answers2

745

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
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
phlogratos
  • 13,234
  • 1
  • 32
  • 37
139

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

apple|banana
smoak
  • 14,554
  • 6
  • 33
  • 33