RedUnited
RedUnited

Reputation: 19

Regular Expressions to match exactly

I currently have this regular expression in my javascript

^[A-Z]{2}-[0-9]{4}\b]

At the moment it will accept any 2 letters in any combination followed by a hyphen and then any 4 digit number again in any combination.

I want to create a regular expression that only accepts either 'AA', 'AB', 'AC', 'BA', 'BB', or 'BC' in the first section and the 4 digit number to start with either a '1', '2', or '3' and any combination for the following 3 digits all this with keeping the hyphen separating the characters and the digits.

Upvotes: 1

Views: 97

Answers (1)

The fourth bird
The fourth bird

Reputation: 163217

You could use a character class for matching A, B or C and 1, 2 and 3 and use a quantifer {3} to match 3 digits 0-9

^[AB][ABC]-[123][0-9]{3}\b

Regex demo

Or use a $ at the end instead of \b if you want to match the whole string.

Upvotes: 2

Related Questions