Reputation: 10216
I want to allow the following pattern: either any number or two numbers with a hyphen between them, so basically a house number.
This works so far but seems a little weird to me: /^(\d)+$|^([\d]+-[\d]+)$/g
This correctly valids "1", "12", "12-13"
and unvalids "-2
" and "12-"
Upvotes: 2
Views: 202
Reputation: 369054
How about following?
/^\d+(-\d+)?$/
g
flag.?
makes following -\d+
part optional.Example:
/^\d+(-\d+)?$/.test('1') // true
/^\d+(-\d+)?$/.test('12') // true
/^\d+(-\d+)?$/.test('12-13') // true
/^\d+(-\d+)?$/.test('-13') // false
/^\d+(-\d+)?$/.test('13-') // false
Upvotes: 2