0

This question here helped me how to do an exception: Regex with exception of particular words

Basically, rx = /^(?!apple$|orange$|banana$)/ would match everything but apple, orange and banana. But now I need to know how to do an exception for an exception.

rx = /^(?!.*$)/

I believe this would ignore everything, but what should I add to make it match nothing BUT some pre-defined words, like banana and apple?

Community
  • 1
  • 1
  • 4
    er... are you just trying to match banana and apple? /^(apple|banana)$/ – Christophe Nov 08 '12 at 19:09
  • 1
    Isn't an exception to an exception just a normal regex? So, to match apple and/or banana (but nothing else), would something simple like `/apple|banana/`work? – Upgradingdave Nov 08 '12 at 19:11

1 Answers1

1

The opposite of the opposite of x is simply x:

/^(apple|orange|banana)$/

The above regex only accepts input which exactly matches apple, orange, or banana.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710