How can i match a expression in which first three characters are alphabets followed by a "-" and than 2 alphabets.
For eg. ABC-XY
Thanks in advance.
How can i match a expression in which first three characters are alphabets followed by a "-" and than 2 alphabets.
For eg. ABC-XY
Thanks in advance.
[A-Z]{3}-[A-Z]{2}
if you also want to allow lowercase, change A-Z to A-Za-z.
If you want only to test if the string matchs the pattern, use the test method:
function isValid(input) {
return /^[A-Z]{3}-[A-Z]{2}$/.test(input);
}
isValid("ABC-XY"); // true
isValid("ABCD-XY"); // false
Basically the /^[A-Z]{3}-[A-Z]{2}$/ RegExp looks for:
^[A-Z]{3}-[A-Z]{2}$If you want to match alphanumeric characters, you can use \w instead of [A-Z].
Resources: