简体   繁体   中英

range of comma separated numbers in regex

I'm trying to test if a string matches the following string format using regex. It doesn't have to be these exact digits but any digits.

"1000-3000,5000-6000,6000-7000"

Below is what I have tried so far:

/[0-9]+(,[0-9]+)*/g

Your regexp matches a sequence of numbers, not ranges, because it doesn't match - between the numbers. \d+-\d+ matches two numbers separated by - .

And you should anchor it if you want to test that the whole string matches, not a substring.

/^\d+-\d+(,\d+-\d+)*$/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM