Reputation: 616
I need a regex which should validate a value which
I did this regex which seems to working but I believe there is a better way of writing this. This is my regex. Please suggest a better way
(^[0-9]{3}$)|(^0[0-9]{3}$)|(^[0-9a-zA-Z]{5}$)
Upvotes: 0
Views: 36
Reputation: 11650
here is one way about it
/^(0?\d{3}|[\D\d]{5})/
https://regex101.com/r/s8Swc0/1
Upvotes: 0
Reputation: 18490
Using an optional 0
can make it shorter and also change to the alphanumeric in the end:
/^(?:0?\d{3}|[a-z\d]{5})$/i
See this demo at regex101 (pattern is already in delimiters with i
flag for ignoring case)
Upvotes: 4