Reputation: 1100
I'm trying to make a regex that matches a string that is exactly 10 characters in length and must start with either "ESZ", "XXZ", "H" or "A2/10". I tried something like this but I can't specify the total string's length because of the length variation in the starting words:
^(ESZ|XXZ|H|A2/10).{5,9}$
How to specify the total string's length to '10' in this case?
Upvotes: 0
Views: 399
Reputation: 3519
Since the alternatives are so few, why not specify them all with ^(ESZ.{7}|XXZ.{7}|H.{9}|A2\/10.{5})$
?
https://regex101.com/r/5BwTWP/1
EDIT:
This can alternatively be achieved by : (?=.{10}$)(ESZ|XXZ|H1|A2\/10)
https://regex101.com/r/5BwTWP/3
This regex first does a lookahead to make sure there are 10 chars in the string. Something like a check for string.length === 10
before regex matching should also do.
I still think the very first approach is the simplest and most efficient.
Upvotes: 2