TwelvestoneFront End

regex gurus


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
Technomancer
 
2009-12-07

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?

Stickman
 
2009-12-07

What can be in between the commas (i.e. what might replace 'choice1', 'choice2'...)? Is it literally 'choice' followed by a number?

the real me
 
2009-12-07

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:

Technomancer
 
2009-12-07

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.

the real me
 
2009-12-07

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

(+k(\w+,?){2,}?k+$

Technomancer
 
2009-12-07

thanks TRM, I'll give that a go.

wowbagger[tip]
 
2009-12-07

+:(\w+,)+\w+)+(;\w+:(\w+,)+\w+)*$

this one doesn't pass with a trailing semicolon though k

the real me
 
2009-12-07

Originally posted by: wowbagger[tip] [quote]+:(\w+,)+\w+)+(;\w+:(\w+,)+\w+)*$

this one doesn't pass with a trailing semicolon though k[/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...

wowbagger[tip]
 
2009-12-07

gah indeed

+:(\w+,)+\w+)(;\w+:(\w+,)+\w+)*$

that should match (with reasonable doubt k )

initial option group followed by zero or more option groups

the real me
 
2009-12-07

nice, that one seems to work well k

Technomancer
 
2009-12-07

Thanks guys, that works a treat :notworthy

Technomancer
 
2009-12-08

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?

wowbagger[tip]
 
2009-12-08

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.

Technomancer
 
2009-12-08

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.

wowbagger[tip]
 
2009-12-08

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

Technomancer
 
2009-12-10

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+))*$

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFront End

regex gurus