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?

2
  • 4
    er... are you just trying to match banana and apple? /^(apple|banana)$/ Commented Nov 8, 2012 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? Commented Nov 8, 2012 at 19:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.