10
I need a Regex for the A-Z 1-9 A-Z standard validating AZ123456789AZ
I’ve tried to /^[[A-Z]{2}[1-9]{9}[A-Z]{2}]/$
10
I need a Regex for the A-Z 1-9 A-Z standard validating AZ123456789AZ
I’ve tried to /^[[A-Z]{2}[1-9]{9}[A-Z]{2}]/$
8
As I said, your regex is almost right, you just need to take the []
unnecessary, making some corrections she would look like this:
^[A-Z]{2}[1-9]{9}[A-Z]{2}$
I took a test on this one link, you can access it and test some modifications as well.
Because with []
doesn’t work?
[ ] defines a group of characters, a-z are all minuscule letters without accentuation, if you put characters there separately, they are included in the [ ] group, so when you put your regex inside it, it was validating character [, ], {, }, 2, A-Z, 1-9, and -. Hence the logic implemented with them was being invalidated.
@Robertodecampos I put an answer explaining why it didn’t work with []
7
Only to complement the user reply @Paz, a brief explanation as to why your attempt did not work.
The 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 catch only one character.
There are some shortcuts like [A-Z]
, meaning "any letter from A to Z", but it is also possible to put other characters together. For example, [3A-Z]
means "the digit 3 or any letter from A to Z".
Inside the brackets it is also possible to put the opening bracket itself, ie, [[]
means "the character [
" (since it is in brackets). See:
console.log(/[[]/.test('[')); // true
Already the closing bracket should be escaped with \
, otherwise regex will understand that the first bracket is being closed:
// "]" escapado com "\"
console.log(/[\]]/.test(']')); // true
Therefore, the first part of its 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 quantifier {2}
, which will accept two occurrences of this regex, followed by 9 digits and 2 letters. And at the end, the last ]
corresponds to the character itself ]
, since there is no equivalent opening bracket (all [
previous have already been closed).
This means that your regex will only accept strings with one ]
at the end (in addition to accepting [
at first):
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 is why it is correct to remove these brackets from the beginning and end. Another point of attention is that in Javascript a regex is delimited by the bars, therefore /$
must be exchanged for $/
:
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 is unclear is whether you need all the digits or not. I say this because [1-9]
considers only the digits from 1 to 9 (that is, it does not accept zero):
console.log(/[1-9]/.test('0')); // false
console.log(/[1-9]/.test('1')); // true
If you also need a zero, just switch to [0-9]
, or simply to \d
:
console.log(/[0-9]/.test('0')); // true
console.log(/\d/.test('0')); // true
I mean, the expression would be:
// 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
Thanks for the explanation, now not only copied the code and pasted as well as I know what I pasted in my code. Congratulations for the explanation understood straight.
what a spectacle of explanation, congratulations!
It is to realize the mistakes we all learn! + 1
5
To the standard AZ123456789AZ
, that’s all it takes:
^[A-Z]{2}\d{9}[A-Z]{2}$
^ e $
, are an edge representing the beginning and end of a, are important because if a letter exceeds the match
will return as far as is valid, so would not fully validate the standard, what I explained can be seen in regex101.[A-Z]
set that takes the letters in the range of A
until Z
, That doesn’t include the lower case.{2}
exactly 2 letters\d
shortcut to the set[0-9]
Browser other questions tagged javascript regex
You are not signed in. Login or sign up in order to post.
Ever tried one? If so, it’s good to ask the question. Read this guide here to avoid receiving down vote on your question.
– Ygor Azevedo
I edited the question
– Murilo Medeiros
its regex works for this, it is only necessary to take the [ at the beginning and ] at the end, thus staying: [A-Z]{2}[1-9]{9}[A-Z]{2}$/g
– Paz
In the end, instead of
/$
, should be$/
– hkotsubo
i did the test here my good, just follow this link: https://regex101.com/r/boeX7h/2/
– Paz
^[A-Z]{2}[\d]{9}[A-Z]{2}$
worked. You can test on https://regexr.com/– Ygor Azevedo
@Ygorazevedo
\d
takes all digits from 0 to 9, but the question implied that he only needs 1 to 9 ([1-9]
)– hkotsubo
I need to mark as solved as you do?
– Murilo Medeiros
Numbers are 1 to 9 or 0 to 9 ?
– Isac
@Murilomedeiros As it was not clear if the numbers are 0 to 9 or 1 to 9, I put an observation on this in my reply. But it seems that the post office accepts zero yes, at least there is an example of code with zero on this page: https://www2.correios.com.br/system/tracking/
– hkotsubo