0

I am using ng-pattern to check for a regular expression The pattern should contain 3 letters and 2 numbers in a group.

Example G-31SSD or G-EEE43

I am currently using pattern which matches only the second one

 ng-model="newGroup.groupCode" ng-pattern="/^\b[gG][-][a-zA-Z]{3}\d{2}\b$/"

How can I make it to check for both the condition or can I write OR condition for ng-pattern

1

2 Answers 2

2

Use alternation | with a non-capturing group (?:...)

^[gG]-(?:[a-zA-Z]{3}\d{2}|\d{2}[a-zA-Z]{3})$

Demo

Sign up to request clarification or add additional context in comments.

Comments

2
^[gG]-\d{2}[a-zA-Z]{3}$|^[gG]-[a-zA-Z]{3}\d{2}$

or you also use

^[gG]-(?:(?:\d{2}[a-zA-Z]{3})|(?:[a-zA-Z]{3}\d{2}))$

Try this.this will fulfill both the conditions.You just have to use an | operator which will match both types.See demo.

http://regex101.com/r/dV8uP2/2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.