There seems to be an issue with the lowercase letters not matching in your single item pattern. To address this, ensure that they are included in the character class, for example changing [A-Z0-9]{0,16}
to [A-Za-z0-9]{0,16}
.
The revised overall solution will appear as follows
/^[A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};(?: [A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};)*$/
For a demonstration and testing of the regex pattern, you can check the regex demo here
Additional Details:
^
- indicates the beginning of the string
[A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};
- specifies the item pattern:
[A-Z]{2}
- two uppercase ASCII letters
[A-Z_]
- either an uppercase ASCII letter or _
_
- represents a _
character
[A-Za-z0-9]{0,16}
- allows for 0 to 16 alphanumeric ASCII characters
;
- indicates a semi-colon
(?: [A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};)*
- denotes zero or more occurrences of
$
- marks the end of the string.
In JavaScript, it is advisable to construct the pattern dynamically for better readability:
var block = "[A-Z]{2}[A-Z_]_[A-Za-z0-9]{0,16};"
var regex = new RegExp(`^${block}(?: ${block})*$`)
console.log( regex.test("ABC_abc123; AB__B2A4; ABF_323WET;") )