数字和短划线的正则表达式 [英] Regular Expression for number and dash

查看:164
本文介绍了数字和短划线的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我写这样的正则表达式: / ^([\d +] * - )+([\ d] * - )+([\ d])* $ /

Currently I write the regex like this: /^([\d+]*-)+([\d]*-)+([\d])*$/

我希望结果遵循这种模式 00-123-456-789 00123456789 (在数字组之间划线或根本没有划线)

I want the result follow this pattern 00-123-456-789 or 00123456789 (dash between number group or no dash at all)


  • 不是 00-123--457-789

  • -00-123-456-789 -

  • -00123456789 -

  • 00- 123-456-789 -

  • not 00-123--457-789
  • or -00-123-456-789-
  • or -00123456789-
  • or 00-123-456-789-

如何修改正则表达式以匹配上述模式?

How can I modify the regex to matches the pattern above?

推荐答案

尝试这样的事情

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

这篇关于数字和短划线的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆