I need to identify strings that have the exact templates (can be either of the two):
- 1 OR 2 Characters (A-Z) and then followed by a number (doesn't matter how many digits).
- K1E OR K2E and then followed by a number (also doesn't matter how many digits). The letter 'K' and then a '1' OR '2' and then followed by an 'E' is strictly how the string should begin.
This is what I have so far:
var word = "K4E43057";
if (word.match(/^[A-Za-z]{1,2}[0-9]/g)
|| word.match(/^[Kk]{1}[12]{1}[Ee]{1}[0-9]/g)) {
alert("It matches.");
}
It's identifying that the string does indeed match, even though the prefix is K4E when it should only be either K1E or K2E. I'm still a little new to regular expressions, so if you could help me out that would be great.
Thank you for your time!
{1,2}in the first regex should just be{2}/^[A-Za-z]{1,2}[12]/And are you really OK matching one or two letters at the beginning?