Reputation: 57
This is the regex that I have made
/^[0-9]+-[0-9]+/
what it does is validates weather a range of numbers is chosen (eg: 1-2 or 4-7)
now I want to add comma separation to this regex such that it accepts strings like (1-2,3-4,... and even 1-2)
I tried doing something like:
/^[0-9]+-[0-9]+(,[0-9]+-[0-9]+)/
But this matches only things like 1-2,3-4 and nothing else.
Upvotes: 0
Views: 16
Reputation: 26037
You are very close on this. Use ?:
for an optional match, like so:
[0-9]+-[0-9]+(?:,[0-9]+-[0-9]+)*
Upvotes: 1