0

Conditions:-

  1. Total length = 12
  2. First letter = A
  3. Second letter = B
  4. Rest of the 10 characters = Numbers or alphabets
  5. Rest of the 10 characters cannot be equal to 0000000000

Valid:-

  1. AB1234567890
  2. ABABABABABAB
  3. AB1234HIJ001

Invalid:-

  1. AB0000000000
  2. AB0
  3. AA1234567890

I have come up with this regex: '^[A-A][B-B][A-Z0-9]{10}$'. It prevents Invalid #2 and #3. But I'm having a hard time with Invalid #1. I know that to prevent all characters from being 0, I need to use '^0+$'. But how do I combine these two expressions?

Sh4dy
  • 143
  • 2
  • 15
  • 1
    Why not do the check for all zeros with a plain JavaScript string comparison? It's likely to be easier to write and to read than a regex solution. – Thomas Mar 31 '22 at 12:34
  • it won't be possible with regex – Vulwsztyn Mar 31 '22 at 12:35
  • @Thomas Because I have an array of regexes which I'm using as eligible formats for an input. I want my Javascript code to match the formats from the array, instead of breaking down the input and checking the format. – Sh4dy Mar 31 '22 at 12:38
  • 4
    @Vulwsztyn Not true. Very definitely possible as a regular expression. – phuzi Mar 31 '22 at 12:48

1 Answers1

3

You can add a negative lookahead to accomplish this: (?!0{10}).

^[A-A][B-B](?!0{10})[A-Z0-9]{10}$
           ^^^^^^^^^

regex101 demo

By the way, you don't need [A-A][B-B]. Just AB does exactly the same.

Thomas
  • 174,939
  • 50
  • 355
  • 478