用于区域代码的javascript正则表达式(javascript regex for region code)

我有一个区域代码验证的正则表达式问题。 我的区域代码只能是一位数字,但它也可以是由' - '分隔的数字

例如,我的区域代码可以是以下之一: 6 6-66 77-7

如你所见,我必须至少有一个数字或数字用' - '分隔,如果它们是分开的,那么' - '符号后面应该有一个数字(无关紧要)。 所以6-不得被验证为合法的区域代码。 我尝试了2个小时来解决这个问题,但我不能,所以请帮助我! 谢谢!

I have a regex problem with validation for a region code. My region code could be only one digit but it also could be a digits separated by '-'

for Example my region code could be one of the following: 6 6-66 77-7

As you can see I must have at least one digit or digits separated by '-' and if they are separated there should be a digits after the '-' sign (does not matter how many). So 6- must not be validated as legal region code. I try 2 hours to solve this, but I couldn't, so please help me! Thank you!

最满意答案

/\d+(-\d+)?$/

这将匹配6,6-66,77-7 , but not 6-`

/\d+(-\d+)?$/

This will match 6, 6-66,77-7, but not6-`

更多推荐