2014-03-12 154 views

回答

4

您可以使用一個character class

var regex = /^[\d-]+$/; 

不過,這也讓比賽像。如果你只想讓像123-456-789但不-123123-123--456投入,那麼你可以使用像

var regex = /^\d+(?:-\d+)*$/; 

說明:

^  # Start of string. 
\d+ # Match a number. 
(?: # Start of a non-capturing group that matches... 
- # a hyphen, 
\d+ # followed by a number 
)* # ...any number of times, including zero. 
$  # End of string 
相關問題