Just looking for a simple javascript regex expression to validate some form input.
The input must be formatted as follows
option:choice1,choice2
but there could be more options, so they are separated by ; like so
option1:choice1,choice2;option2:choice3,choice4
any ideas as the best way to approach this?
What can be in between the commas (i.e. what might replace 'choice1', 'choice2'...)? Is it literally 'choice' followed by a number?
i took a quick stab at it, of course this will only work exactly as you have it.
(option[0-9]+:(choice[0-9]+,)+;)+
just a start, but it's something to work with. one thing that is likely to screw up on you is the semi-colon. I think this will match when there are multiple option groups even if it's missing. :shrug:
Originally posted by: Stickman What can be in between the commas (i.e. what might replace 'choice1', 'choice2'...)? Is it literally 'choice' followed by a number?
my bad I should have been a bit more specific. Both choice and option are user input e.g
Colour:Blue,Pink,Green,Yellow;Emblem:Unicorn,Swastika
what I want to validate is that each option:choices pairing/grouping is formatted as a minimum of 2 choices per option and there can be as many option:choices* pairing as they like. I'm not really interested in what the pairings are, just that the input is formatted correctly.
It would make it a lot easier if you could require a semi-colon at the end of the lines as well? If you have the possibility to do that something like this would probably work
(+
(\w+,?){2,}?
+$
thanks TRM, I'll give that a go.
+:(\w+,)+\w+)+(;\w+:(\w+,)+\w+)*$
this one doesn't pass with a trailing semicolon though 
Originally posted by: wowbagger[tip] [quote]+:(\w+,)+\w+)+(;\w+:(\w+,)+\w+)*$
this one doesn't pass with a trailing semicolon though
[/quote]
That's sort of the reason I asked about adding the trailing semicolon. That works except that something with two sets and missing a semicolon like this will still pass won't it?
option1:choice1,choice2option2:choice3,choice4
//edit
i should add that i doubt the one i posted is perfect either. so if you see any thing it'll miss...
gah indeed
+:(\w+,)+\w+)(;\w+:(\w+,)+\w+)*$
that should match (with reasonable doubt
)
initial option group followed by zero or more option groups
nice, that one seems to work well 
Thanks guys, that works a treat :notworthy
Originally posted by: Technomancer Thanks guys, that works a treat :notworthy
ok I lied a bit - that nearly works perfectly. if an option group contains a space it fails e.g.
Colour:White,Light Blue,Dark Blue
How i do amend the regex to allow spaces between words? Or am I asking too much?
Untested : switch any \w+ for (\w[\w\s]*)+.
That does allow for "SwastikaPink", "Swastika_P_i____ n__ k", "S_____" etc though. ( where underscores are whitespace ). If it has to restrict to single whitespaces that aren't trailing it becomes a bit more complicated.
Thanks again, that'll probably suffice.
How complicated is 'a bit more complicated'? Just in case I need to restrict to 1 non-trailing whitespace.
I can maybe take a look tonight, but I'm guessing something like :
\w(\w|\s\w+)*
a character followed by either ( a character ) or ( a whitespace followed by at least 1 character), where both additions can exist zero or more times.
/edit I'm stupid
\w+(\s\w+)?
I think this will do it
sorry,
what would my final regex expression look like?
becaue when i substitute
\w+ with \w+(\s\w+)?
it doesn't validate
my regex:
+:(\w+(\s\w+),)+\w+(\s\w+))(;\w+:(\w+(\s\w+),)+\w+(\s\w+))*$
EDIT:
This one works: +:(\w(\w|\s\w+),)+\w(\w|\s\w+))(;\w+:(\w(\w|\s\w+),)+\w(\w|\s\w+))*$