You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That was a character class for digits. There are other character classes as well.
36
+
Essa era uma classe de caracteres para dígitos. Existem outras classes de caracteres.
37
37
38
-
Most used are:
38
+
As mais usadas são:
39
39
40
-
`pattern:\d` ("d" is from "digit")
41
-
: A digit: a character from`0`to`9`.
40
+
`padrão:\d` ("d" é de "digit")
41
+
: Um dígito: um caractere de`0`a`9`.
42
42
43
-
`pattern:\s` ("s" is from "space")
44
-
: A space symbol: includes spaces, tabs`\t`, newlines `\n`and few other rare characters, such as `\v`, `\f`and`\r`.
43
+
`padrão:\s` ("s" é de "space")
44
+
: Um símbolo de espaço: inclui espaços, tabulações`\t`, novas linhas `\n`e alguns outros caracteres raros, como `\v`, `\f`e`\r`.
45
45
46
-
`pattern:\w` ("w" is from "word")
47
-
: A "wordly" character: either a letter of Latin alphabet or a digit or an underscore `_`. Non-Latin letters (like cyrillic or hindi) do not belong to `pattern:\w`.
46
+
`padrão:\w` ("w" é de "word" (*palavra*))
47
+
: Um caractere de texto: uma letra do alfabeto latino ou um dígito ou um sublinhado `_`. Letras não latinas (como cirílico ou hindu) não pertencem ao `padrão:\w`.
48
48
49
-
For instance, `pattern:\d\s\w`means a "digit" followed by a "space character" followed by a "wordly character", such as `match:1 a`.
49
+
Por exemplo, `padrão:\d\s\w`significa um "dígito" seguido de um "caractere de espaço" seguido de um "caractere de texto", como `correspondência:1 a`.
50
50
51
-
**A regexp may contain both regular symbols and character classes.**
51
+
**Uma regexp pode conter símbolos regulares e classes de caracteres.**
52
52
53
-
For instance, `pattern:CSS\d`matches a string `match:CSS`with a digit after it:
53
+
Por exemplo, `padrão:CSS\d`corresponde a uma string `correspondência:CSS`com um dígito a seguir:
54
54
55
55
```js run
56
-
let str ="Is there CSS4?";
56
+
let str ="Existe CSS4?";
57
57
let regexp =/CSS\d/
58
58
59
59
alert( str.match(regexp) ); // CSS4
60
60
```
61
61
62
-
Also we can use many character classes:
62
+
Também podemos usar muitas classes de caracteres:
63
63
64
64
```js run
65
-
alert( "I love HTML5!".match(/\s\w\w\w\w\d/) ); // ' HTML5'
65
+
alert( "Eu amo HTML5!".match(/\s\w\w\w\w\d/) ); // ' HTML5'
66
66
```
67
67
68
-
The match (each regexp character class has the corresponding result character):
68
+
A correspondência (cada classe de caracteres regexp possui o caractere de resultado correspondente):
69
69
70
70

71
71
72
-
## Inverse classes
72
+
## Classes inversas
73
73
74
-
For every character class there exists an "inverse class", denoted with the same letter, but uppercased.
74
+
Para cada classe de caracteres existe uma "classe inversa", denotada com a mesma letra, mas em maiúsculas.
75
75
76
-
The "inverse" means that it matches all other characters, for instance:
76
+
O "inverso" significa que ele corresponde a todos os outros caracteres, por exemplo:
77
77
78
-
`pattern:\D`
79
-
: Non-digit: any character except `pattern:\d`, for instance a letter.
78
+
`padrão:\D`
79
+
: Não-dígito: qualquer caractere, exceto `padrão:\d`, por exemplo, uma letra.
80
80
81
-
`pattern:\S`
82
-
: Non-space: any character except `pattern:\s`, for instance a letter.
81
+
`padrão:\S`
82
+
: Não-espaço: qualquer caractere, exceto `padrão:\s`, por exemplo, uma letra.
83
83
84
-
`pattern:\W`
85
-
: Non-wordly character: anything but `pattern:\w`, e.g a non-latin letter or a space.
84
+
`padrão:\W`
85
+
: Caractere não verbal: qualquer coisa, exceto `padrão:\w`, por exemplo, uma letra não latina ou um espaço.
86
86
87
-
In the beginning of the chapter we saw how to make a number-only phone number from a string like`subject:+7(903)-123-45-67`: find all digits and join them.
87
+
No início do capítulo, vimos como criar um número de telefone somente com números a partir de uma string como`subject:+7(903)-123-45-67`: encontre todos os dígitos e os junte.
An alternative, shorter way is to find non-digits `pattern:\D`and remove them from the string:
95
+
Uma maneira alternativa e mais curta é encontrar não-dígitos `padrão:\D`e removê-los da string:
96
96
97
97
```js run
98
98
let str ="+7(903)-123-45-67";
99
99
100
100
alert( str.replace(/\D/g, "") ); // 79031234567
101
101
```
102
102
103
-
## A dot is "any character"
103
+
## Um ponto é "qualquer caractere"
104
104
105
-
A dot `pattern:.`is a special character class that matches "any character except a newline".
105
+
Um ponto `padrão:.`é uma classe de caracteres especial que corresponde a "qualquer caractere, exceto uma nova linha".
106
106
107
-
For instance:
107
+
Por exemplo:
108
108
109
109
```js run
110
110
alert( "Z".match(/./) ); // Z
111
111
```
112
112
113
-
Or in the middle of a regexp:
113
+
Ou no meio de uma regexp:
114
114
115
115
```js run
116
116
let regexp =/CS.4/;
117
117
118
118
alert( "CSS4".match(regexp) ); // CSS4
119
119
alert( "CS-4".match(regexp) ); // CS-4
120
-
alert( "CS 4".match(regexp) ); // CS 4 (space is also a character)
120
+
alert( "CS 4".match(regexp) ); // CS 4 (o espaço é também um caractere)
121
121
```
122
122
123
-
Please note that a dot means "any character", but not the "absence of a character". There must be a character to match it:
123
+
Observe que um ponto significa "qualquer caractere", mas não a "ausência de um caractere". Deve haver um caractere para corresponder a ele:
124
124
125
125
```js run
126
-
alert( "CS4".match(/CS.4/) ); // null, no match because there's no character for the dot
126
+
alert( "CS4".match(/CS.4/) ); // null, sem correspondência porque não há caractere para o ponto
127
127
```
128
128
129
-
### Dot as literally any character with "s" flag
129
+
### Ponto como literalmente qualquer caractere com a flag "s"
130
130
131
-
By default, a dot doesn't match the newline character`\n`.
131
+
Por padrão, um ponto não corresponde ao caractere de nova linha`\n`.
132
132
133
-
For instance, the regexp `pattern:A.B`matches `match:A`, and then `match:B`with any character between them, except a newline`\n`:
133
+
Por exemplo, a regexp `padrão:A.B`corresponde `corresponde:A` e, em seguida, `corresponde:B`com qualquer caractere entre eles, exceto uma nova linha`\n`:
134
134
135
135
```js run
136
-
alert( "A\nB".match(/A.B/) ); // null (no match)
136
+
alert( "A\nB".match(/A.B/) ); // null (sem correspondência)
137
137
```
138
138
139
-
There are many situations when we'd like a dot to mean literally "any character", newline included.
139
+
Há muitas situações em que gostaríamos que um ponto significasse literalmente "qualquer caractere", incluindo a nova linha.
140
140
141
-
That's what flag `pattern:s`does. If a regexp has it, then a dot `pattern:.`matches literally any character:
141
+
É o que a flag `padrão:s`faz. Se uma regexp a possui, então um ponto `padrão:.`corresponde literalmente a qualquer caractere:
Luckily, there's an alternative, that works everywhere. We can use a regexp like `pattern:[\s\S]` to match "any character" (this pattern will be covered in the article <info:regexp-character-sets-and-ranges>).
150
+
Felizmente, há uma alternativa, que funciona em qualquer lugar. Podemos usar uma regexp como `padrão:[\s\S]` para corresponder a "qualquer caractere" (este padrão irá ser estudado no artigo <info:regexp-character-sets-and-ranges>).
The pattern `pattern:[\s\S]` literally says: "a space character OR not a space character". In other words, "anything". We could use another pair of complementary classes, such as `pattern:[\d\D]`, that doesn't matter. Or even the `pattern:[^]` -- as it means match any character except nothing.
156
+
O padrão `padrão:[\s\S]` diz literalmente: "um caractere de espaço OU não um caractere de espaço". Em outras palavras, "qualquer coisa". Poderíamos usar outro par de classes complementares, como `padrão:[\d\D]`, que não importa. Ou mesmo o padrão `padrão:[^]` -- pois significa corresponder a qualquer caractere, exceto nada.
157
157
158
-
Also we can use this trick if we want both kind of "dots" in the same pattern: the actual dot `pattern:.` behaving the regular way ("not including a newline"), and also a way to match "any character" with `pattern:[\s\S]` or alike.
158
+
Também podemos usar esse truque se quisermos os dois tipos de "pontos" no mesmo padrão: o ponto real `padrão:.` comportando-se da maneira regular ("não incluindo uma nova linha") e também uma maneira de combinar "qualquer caractere" com `padrão:[\s\S]` ou similar.
159
159
````
160
160
161
-
````warn header="Pay attention to spaces"
162
-
Usually we pay little attention to spaces. For us strings `subject:1-5` and `subject:1 - 5` are nearly identical.
161
+
````warn header="Preste atenção aos espaços"
162
+
Geralmente prestamos pouca atenção aos espaços. Para nós, as strings `sujeito:1-5` e `sujeito:1 - 5` são quase idênticas.
163
163
164
-
But if a regexp doesn't take spaces into account, it may fail to work.
164
+
Mas se uma regexp não leva em consideração os espaços, ela pode falhar.
165
165
166
-
Let's try to find digits separated by a hyphen:
166
+
Vamos tentar encontrar dígitos separados por um hífen:
167
167
168
168
```js run
169
-
alert( "1 - 5".match(/\d-\d/) ); // null, no match!
169
+
alert( "1 - 5".match(/\d-\d/) ); // null, sem correspondência!
170
170
```
171
171
172
-
Let's fix it adding spaces into the regexp `pattern:\d - \d`:
172
+
Vamos corrigi-lo adicionando espaços à regexp `padrão:\d - \d`:
173
173
174
174
```js run
175
-
alert( "1 - 5".match(/\d - \d/) ); // 1 - 5, now it works
176
-
// or we can use \s class:
177
-
alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, also works
-`padrão:.` -- qualquer caractere se estiver com a flag regexp `'s'`; caso contrário, qualquer um, exceto uma nova linha`\n`.
198
198
199
-
...But that's not all!
199
+
...Mas isso não é tudo!
200
200
201
-
Unicode encoding, used by JavaScript for strings, provides many properties for characters, like: which language the letter belongs to (if it's a letter), is it a punctuation sign, etc.
201
+
A codificação Unicode, usada pelo JavaScript para strings, fornece muitas propriedades para caracteres, como: a qual idioma a letra pertence (se for uma letra), é um sinal de pontuação, etc.
202
202
203
-
We can search by these properties as well. That requires flag `pattern:u`, covered in the next article.
203
+
Também podemos pesquisar por essas propriedades. Isso requer a flag `padrão:u`, abordada no próximo artigo.
0 commit comments