1

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.

askedNov 19, 2009 at 6:21
pks83's user avatar
1
  • That very sentence pretty much spells out the answer for you.... if you know regexes at all.CommentedNov 19, 2009 at 6:33

4 Answers4

4

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"); // false

Basically 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ó's user avatar
Sign up to request clarification or add additional context in comments.

Comments

1
[A-Z]{3}-[A-Z]{2}

if you also want to allow lowercase, changeA-Z toA-Za-z.

answeredNov 19, 2009 at 6:24
Amber's user avatar

Comments

0
/^[a-zA-Z]{3}-[a-zA-Z]{2}$/
answeredNov 19, 2009 at 6:25
Asaph's user avatar

Comments

0
/\w{3}-\w{2}/.test("ABC-XY")true

it will match A-Za-z_ though.

answeredNov 19, 2009 at 6:30
YOU's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.