Reputation: 179
I have a problem with creating regexp to allow:
so example of valid string is GB123-55-22-22-6
,
my current regexp is: /^([A-Z]{2})?[0-9]{10}$/
. He allow GB1235522226
but I have a problem with "-".
can somoene tell me how to allow this regexp to use at most 4 "-" chars?
thanks for any help!
Upvotes: 2
Views: 21
Reputation: 626748
You can use
^(?:[A-Z]{2})?(?=(?:-?\d){10}$)[0-9]+(?:-[0-9]+){0,4}$
See the regex demo.
Details:
^
- start of string(?:[A-Z]{2})?
- optional two letters(?=(?:-?\d){10}$)
- there must be 10 digits optionally separated with a -
till end of string, the string must end with a digit[0-9]+
- one or more digits(?:-[0-9]+){0,4}
- zero to four occurrences of a hyphen and then one or more digits$
- end of string.Upvotes: 1