noctilux noctilux - 11 days ago 5
Javascript Question

Regular Expression for number and dash

Currently I write the regex like this:

/^([\d+]*-)+([\d]*-)+([\d])*$/


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


  • 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?

Answer

Try something like this

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