regex - Is there a regular expression for a comma separated list of discrete values? -


I use the following regular expression to validate a comma separated list of values.

  ^ Dog | Cat | Bird | The values ​​are listed in a drop-down list in Excel (the (dog | cat | bird | mouse)) $ $  

value, so users can select a value from the dropdown list , Or can be typed in different values ​​by commas.

Regular expression does a good job of preventing the user from entering anything but the accepted values, but this prevents the user from entering the duplicate, for example, the user "dog" and The "dog, cat" can enter, but the user can also enter "dog, dog".

Does the duplicate stop using the same regular regular expression? In other words, I should be able to apply the discrete list of values ​​separated by the comma separated.

Thank you!

Use a backtraction and a negative perspective:

  ^ (Dog | cat | bird | mouse) (, (?!! 1) (dog | cat | bird | mouse)) * $  

edit : This will not work with cases such as "cat, dog, dog" ... you will need to come up with a hybrid solution for such examples - I do not believe there is a single reggae that can control it Is.


Here's another technique you need to check two things, first of all, it matches:

  (?: (?: ^ |,) (Dog | cat | bird | mouse)) + $  

(this is just a small version of your original regex)

Again, check that it does not match:

  (dog | cat | bird | mouse) +? Example: <1  

example

  var valid = string.match (/ (?: (?: ^ |,) (Dog | cat | bird | mouse )) + $ /) & Amp; Amp; ! String.match (/ (dog catbird.mouse). +++++);  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -