I need a Regex for the default A-Z 1-9 A-Z validating AZ123456789AZ
I've tried /^[[A-Z]{2}[1-9]{9}[A-Z]{2}]/$
I need a Regex for the default A-Z 1-9 A-Z validating AZ123456789AZ
I've tried /^[[A-Z]{2}[1-9]{9}[A-Z]{2}]/$
As I said, your regex is almost right, you just need to remove the% unnecessary%, making some corrections it would look like this:
^[A-Z]{2}[1-9]{9}[A-Z]{2}$
I did a test on this link , you can access it and test some modifications as well.
Just to complement the @Paz user response , a brief explanation of why your attempt did not work.
Brackets define a class or character set . For example, [ABC]
means "the letter A or the letter B or the letter C". It is an expression that will take only one character.
There are some shortcuts like [A-Z]
, which means "any letter from A to Z", but you can also put other characters together. For example, [3A-Z]
means "the 3 digit or any letter from A to Z".
Within the brackets you can also place the opening bracket itself, that is, [[]
means " [
" (since it is enclosed in square brackets). See:
console.log(/[[]/.test('[')); // true
Now the closing bracket should be escaped with \
, otherwise regex will understand that the first bracket is being closed:
// "]" escapado com "\"
console.log(/[\]]/.test(']')); // true
So, the first part of your regex ( [[A-Z]
) means "the character [
or a letter from A to Z":
console.log(/[[A-Z]/.test('A')); // true
console.log(/[[A-Z]/.test('[')); // true
Next, you place the% cos_de% quantifier, which will accept two occurrences of this regex, followed by 9 digits and 2 letters. And in the end, the last {2}
corresponds to the ]
character itself, since there is no equivalent opening bracket (all ]
has already been closed).
This means that your regex will only accept strings with [
at the end (in addition to accepting ]
at start):
console.log(/^[[A-Z]{2}[1-9]{9}[A-Z]{2}]$/.test('[A123456789AB]')); // true
console.log(/^[[A-Z]{2}[1-9]{9}[A-Z]{2}]$/.test('AZ123456789AB')); // false
That's why it's the right thing to remove these brackets from the beginning and the end. Another point of attention is that in JavaScript a regex is delimited by the slashes, so [
must be replaced by /$
:
console.log(/^[A-Z]{2}[1-9]{9}[A-Z]{2}$/.test('[A123456789AB]')); // false
console.log(/^[A-Z]{2}[1-9]{9}[A-Z]{2}$/.test('AZ123456789AB')); // true
Another point that has not been clear is whether you need all the digits or not. I say this because $/
considers only digits 1 through 9 (ie, does not accept zero):
console.log(/[1-9]/.test('0')); // false
console.log(/[1-9]/.test('1')); // true
If you also need zero, simply change to [1-9]
, or simply to [0-9]
:
console.log(/[0-9]/.test('0')); // true
console.log(/\d/.test('0')); // true
That is, the expression would look like:
// aceitar todos os dígitos (incluindo o zero)
console.log(/^[A-Z]{2}\d{9}[A-Z]{2}$/.test('AZ123456789AB')); // true
console.log(/^[A-Z]{2}\d{9}[A-Z]{2}$/.test('AZ023456789AB')); // true
For the AZ123456789AZ
pattern, this is enough:
^[A-Z]{2}\d{9}[A-Z]{2}$
^ e $
, are a border that represents the beginning and end of one, are important because if a letter exceeds match
will return to where it is valid, so it would not fully validate the pattern, which I explained can be seen in < a href="https://regex101.com/r/vbcOE3/1"> regex101 . [A-Z]
to A
, this does not include lowercase. Z
exactly 2 letters {2}
shortcut to set \d