Regex "digit-digit"


I am trying to create regex that will parse digits input like: digit1 - digit2 OR digit1 - digit2,digit3 - digit4,digit5 - digit6,digit7 - digit8…
With examples:

ex1. 1-3
ex2. 1-3,5-7
ex3. 1-3,5-7,10-15
ex4. 1-3,7-10,12-15,19-25

Please note that last char should not be “,” and there should not be two or more “-” like 1-5-10!

If anyone can help me with JavaScript regex, thanks in advance!


felgall thank you, this is the real solution!
while I am here, may I ask you what I should change if I add plus any digit before or after… something like…

ex1: 1,3-5,8-19,25,35,43
ex2: 7,12-15,20,29,30-35,39-43
or may be just digits like 4,8,11,17

That would be


This will for example also accept: 1,2,3,4,5-15,18

Please note that this only checks on syntax, not correctness. i.e., 1-3,5-2,1,9-6 would be accepted just fine, while the range itself is completely weird (and can be rewritten to 1-9).