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.
- That very sentence pretty much spells out the answer for you.... if you know regexes at all.mpen– mpen2009-11-19 06:33:36 +00:00CommentedNov 19, 2009 at 6:33
4 Answers4
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"); // trueisValid("ABCD-XY"); // falseBasically the/^[A-Z]{3}-[A-Z]{2}$/ RegExp looks for:
- The beginning of the string
^ - Three uppercase letters
[A-Z]{3} - A dash literally
- - Two more uppercase letters
[A-Z]{2} - And the end of the string
$
If you want to match alphanumeric characters, you can use\w instead of[A-Z].
Resources:
answeredNov 19, 2009 at 6:28
Christian C. Salvadó
831k185 gold badges929 silver badges845 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
[A-Z]{3}-[A-Z]{2}if you also want to allow lowercase, changeA-Z toA-Za-z.
Comments
/^[a-zA-Z]{3}-[a-zA-Z]{2}$/Comments
/\w{3}-\w{2}/.test("ABC-XY")trueit will match A-Za-z_ though.
Comments
Explore related questions
See similar questions with these tags.
