CyberJunkie
CyberJunkie

Reputation: 22674

Regex to match one of two words

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?

Upvotes: 540

Views: 577677

Answers (2)

phlogratos
phlogratos

Reputation: 13924

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

Upvotes: 805

smoak
smoak

Reputation: 15024

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

apple|banana

Upvotes: 148

Related Questions