DawnDreamer
DawnDreamer

Reputation: 9

Regex for an exception within an exception

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?

Upvotes: 0

Views: 76

Answers (1)

Matt Ball
Matt Ball

Reputation: 359966

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.

Upvotes: 1

Related Questions