RegExp: Match '3' words of a list, not repeating

I'm doing an applications in which the user is asked to write, for example, three words of a list (the order is not important).

I can try something like this:

\^ [ itemA | itemB | itemC | itemD | item E | item F] {3}

But obviously, the user can write three times the same item. Is there a way to archieve it using a regular expression and evaluate it using a single "match" function?

I'm using JavaScript, of course.

Thank you guys!


Excuse me, let me clarify.

I have the following screen:

Example

So the user have 7 possible answers, and he have to write three of them.

Of course, this "screen" have a LOT of kind of questions in which a regex worked very well to evaluate it, but in this one, I don't know if it is enought.

Thank you guys.

Upvotes: 0

Views: 99

Answers (1)

Steve Bennett
Steve Bennett

Reputation: 126215

To check that I've understood, you're attempting to validate that:

  1. The user has entered exactly three items,
  2. Each item is a member of a pre-defined list
  3. And each item is unique?

That's not a good fit for a regular expression. You can meet the first two requirements, but it's much simpler to do the third one directly using JavaScript or something similar.

If you really want to do it this way, you can do this:

^(itemA|itemB|itemC|itemD|item E|itemF) (?!\1)(itemA|itemB|itemC|itemD|item E|itemF) (?!\1)(?!\2)(itemA|itemB|itemC|itemD|item E|itemF)

This uses a negative lookahead:

  1. First item must match itemA-itemF
  2. Second item must first not match whatever the first item was, and also match itemA-itemF
  3. Third item must not match the first item, not match the second item, and also match itemA-itemF.

Upvotes: 1

Related Questions