Pruthvi Shetty
Pruthvi Shetty

Reputation: 57

Regex Help - Need to add optional comma seperation to already existing regex

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

Answers (1)

Austin
Austin

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

Related Questions