skip to Main Content

I am trying to create regular expression for specific code, here are the requirements

  1. Length should be 13
  2. First letter starts with X
  3. Next letters A-Z and 0-9 should accept
  4. Only capital letters

Ex : XAABBCC88EE00

I created this expression ^[X]{1}-[a-zA-Z0-9]{12}$, But it’s not working. How Can I create one for this

4

Answers


  1. use ^X[A-Z0-9]{12}$, this would be work for you

    Login or Signup to reply.
  2. ^X[A-Z0-9]{12}$
    

    Here is what it does:

    • ^ asserts start of a line.
    • X the code should start with the letter "X".
    • [A-Z0-9]{12} ensures that the next 12 characters can be any
      uppercase letter (A-Z) or digit (0-9).
    • $ asserts end of a line.

    This regular expression ensures that the code is exactly 13 characters long, starts with "X", and the remaining 12 characters are either uppercase letters or digits.

    Login or Signup to reply.
  3. As per my understanding your thought process for your regex is to define first character length and then other characters length which is not required

    If you use ^X[A-Z0-9]{12}$ it will work, below is the explanation

    ^ Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled. This matches a position, not a character

    X Character matches "X" character (Case sensitive)

    A-Z and 0-9 Matches the exact character A-Z and 0-9 (Case sensitive)

    {12} Quantifier matches 12 digits allowed after X

    Login or Signup to reply.
  4. Hello I hope you are doing fine 🙂

    So you have made a mistake in your pattern ^[X]{1}-[a-zA-Z0-9]{12}$

    The mistake:- you wrote [a-zA-Z0-9] this will accept small and capital alphabets plus the numbers.

    The Solution:-
    The required pattern should be /^[X]{1}[A-Z0-9]{12}$/, this will accept the input as per your needs 😉

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search