Regex number and hyphen

前端 未结 3 1785
傲寒
傲寒 2021-01-18 20:57

I\'m trying to match number with regular expression like:

34-7878-3523-4233

with this:

^[0-9][0-9-]*-[0-9-]*[0-9]$
<         


        
3条回答
  •  醉酒成梦
    2021-01-18 21:43

    That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.

    You can try something like this: -

    ^([0-9]+-)*[0-9]+$
    

    Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.

提交回复
热议问题