Why sets with intervals of A-z returns symbols in REGEX?

5

Scenario:

const texto = 'ABC [abc] a-c 1234';

console.log(texto.match(/[A-z]/g))
  • Because the set of A(maiúsculo) to z(minusculo) , that is, /[A-z]/g returned me [ and ] ?
  • The result should not be: [A, B, C, a, b, c, a, c] ?
asked by anonymous 10.03.2018 / 21:58

2 answers

8

[A-z] will match ASCII characters in sequence from A to z . If you look at the ASCII table below you will find that there are several other characters between A and z ( including the brackets [] that you do not want).

Sinceyouwanttomatchonlyuppercaseandlowercaseletters,fromAtoZandfromatoz,thecorrectwouldbe[a-zA-Z].

const texto = 'ABC [abc] a-c 1234';
console.log(texto.match(/[a-zA-Z]/g))
    
10.03.2018 / 22:27
5

The range or ranges follow the table Unicode .

I have defined that my set should be A(maiúsculo) até z(minusculo) , there are symbols in the middle of that range. They are: [\] ^ _ '

Look at the Unicode table:

Note:

  

The first 127 characters of the Unicode table are the same as the table   ASCII

    
12.03.2018 / 12:59