[英]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" “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 -
. \d+-\d+
匹配由-
分隔的两个数字。
And you should anchor it if you want to test that the whole string matches, not a substring.如果您想测试整个字符串是否匹配,而不是 substring,您应该锚定它。
/^\d+-\d+(,\d+-\d+)*$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.