Regular Expression : Match Either Of Two Conditions?
Hi I don't know much about regular expression. But I need it in form validation using angularJs. Below is the requirement The input box should accept only if either  (1) first 2 le
Solution 1:
In your regex, the two alternative branches are anchored separately:
- (^([a-zA-Z]){2}([0-9]){6})- 2 letters and 6 digits at the start of the string
- |- or
- ([0-9]*)?$- optional zero or more digits at the end of the string
You need to adjust the boundaries of the group:
data-ng-pattern="/^([a-zA-Z]{2}[0-9]{6}|[0-9]{8})?$/"
                   ^                         ^^^^ 
See the regex demo.
Now, the pattern will match:
- ^- start of string
- (- start of the grouping:- [a-zA-Z]{2}[0-9]{6}- 2 letters and 6 digits
- |- or
- [0-9]{8}- 8 digits
 
- )?- end of the grouping and- ?quantifier makes it match 1 or 0 times (optional)
- $- end of string.
Solution 2:
You can try this DEMO LINK HERE
^(([a-zA-Z]{2}|[0-9]{2})[0-9]{6})?$
It will accept:
- ab123456
- 12345678
- aa441236
- aw222222
Post a Comment for "Regular Expression : Match Either Of Two Conditions?"