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
Let's say we have a string like `+7(903)-123-45-67`and want to find all numbers in it. But unlike before, we are interested not in single digits, but full numbers: `7, 903, 123, 45, 67`.
3
+
Digamos que tenemos una cadena como `+7(903)-123-45-67`y queremos encontrar todos los números en ella. Pero contrastando el ejemplo anterior, no estamos interesados en un solo dígito, sino en números completos: `7, 903, 123, 45, 67`.
4
4
5
-
A number is a sequence of 1 or more digits`pattern:\d`. To mark how many we need, we can append a *quantifier*.
5
+
Un número es una secuencia de 1 o más dígitos`pattern:\d`. Para marcar cuántos necesitamos, podemos agregar un *cuantificador*.
6
6
7
-
## Quantity {n}
7
+
## Cantidad {n}
8
8
9
-
The simplest quantifier is a number in curly braces: `pattern:{n}`.
9
+
El cuantificador más simple es un número entre llaves: `pattern:{n}`.
10
10
11
-
A quantifier is appended to a character (or a character class, or a `[...]` set etc) and specifies how many we need.
11
+
Se agrega un cuantificador a un carácter (o a una clase de caracteres, o a un conjunto `[...]`, etc) y especifica cuántos necesitamos.
12
12
13
-
It has a few advanced forms, let's see examples:
13
+
Tiene algunas formas avanzadas, veamos los ejemplos:
14
14
15
-
The exact count: `pattern:{5}`
16
-
: `pattern:\d{5}`denotes exactly 5 digits, the same as`pattern:\d\d\d\d\d`.
15
+
El recuento exacto: `pattern:{5}`
16
+
: `pattern:\d{5}`Denota exactamente 5 dígitos, igual que`pattern:\d\d\d\d\d`.
17
17
18
-
The example below looks for a 5-digit number:
18
+
El siguiente ejemplo busca un número de 5 dígitos:
19
19
20
20
```js run
21
-
alert( "I'm 12345 years old".match(/\d{5}/) ); // "12345"
21
+
alert( "Tengo 12345 años de edad".match(/\d{5}/) ); // "12345"
22
22
```
23
23
24
-
We can add `\b` to exclude longer numbers: `pattern:\b\d{5}\b`.
24
+
Podemos agregar `\b` para excluir números largos: `pattern:\b\d{5}\b`.
25
25
26
-
The range: `pattern:{3,5}`, match 3-5 times
27
-
: To find numbers from 3 to 5 digits we can put the limits into curly braces: `pattern:\d{3,5}`
26
+
El rango: `pattern:{3,5}`, coincide 3-5 veces
27
+
: Para encontrar números de 3 a 5 dígitos, podemos poner los límites en llaves: `pattern:\d{3,5}`
28
28
29
29
```js run
30
-
alert( "I'm not 12, but 1234 years old".match(/\d{3,5}/) ); // "1234"
30
+
alert( "No tengo 12, sino 1234 años de edad".match(/\d{3,5}/) ); // "1234"
31
31
```
32
32
33
-
We can omit the upper limit.
33
+
Podemos omitir el límite superior
34
34
35
-
Then a regexp `pattern:\d{3,}` looks for sequences of digits of length `3` or more:
35
+
Luego, una regexp `pattern:\d{3,}` busca secuencias de dígitos de longitud `3` o más:
36
36
37
37
```js run
38
-
alert( "I'm not 12, but 345678 years old".match(/\d{3,}/) ); // "345678"
38
+
alert( "No tengo 12, sino, 345678 años de edad".match(/\d{3,}/) ); // "345678"
39
39
```
40
40
41
-
Let's return to the string`+7(903)-123-45-67`.
41
+
Volvamos a la cadena`+7(903)-123-45-67`.
42
42
43
-
A number is a sequence of one or more digits in a row. So the regexp is`pattern:\d{1,}`:
43
+
Un número es una secuencia de uno o más dígitos continuos. Entonces la expresión regular es`pattern:\d{1,}`:
44
44
45
45
```js run
46
46
let str ="+7(903)-123-45-67";
@@ -50,14 +50,14 @@ let numbers = str.match(/\d{1,}/g);
50
50
alert(numbers); // 7,903,123,45,67
51
51
```
52
52
53
-
## Shorthands
53
+
## Abreviaciones
54
54
55
-
There are shorthands for most used quantifiers:
55
+
Hay abreviaciones para los cuantificadores más usados:
56
56
57
57
`pattern:+`
58
-
: Means "one or more", the same as`pattern:{1,}`.
58
+
: Significa "uno o más", igual que`pattern:{1,}`.
59
59
60
-
For instance, `pattern:\d+` looks for numbers:
60
+
Por ejemplo, `pattern:\d+` busca números:
61
61
62
62
```js run
63
63
let str = "+7(903)-123-45-67";
@@ -66,77 +66,77 @@ There are shorthands for most used quantifiers:
66
66
```
67
67
68
68
`pattern:?`
69
-
: Means "zero or one", the same as `pattern:{0,1}`. In other words, it makes the symbol optional.
69
+
: Significa "cero o uno", igual que `pattern:{0,1}`. En otras palabras, hace que el símbolo sea opcional.
70
70
71
-
For instance, the pattern `pattern:ou?r` looks for `match:o` followed by zero or one `match:u`, and then `match:r`.
71
+
Por ejemplo, el patrón `pattern:ou?r` busca `match:o` seguido de cero o uno `match:u`, y luego `match:r`.
72
72
73
-
So, `pattern:colou?r` finds both `match:color` and `match:colour`:
73
+
Entonces, `pattern:colou?r` encuentra ambos `match:color` y `match:colour`:
74
74
75
75
```js run
76
-
let str = "Should I write color or colour?";
76
+
let str = "¿Debo escribir color o colour?";
77
77
78
78
alert( str.match(/colou?r/g) ); // color, colour
79
79
```
80
80
81
81
`pattern:*`
82
-
: Means "zero or more", the same as `pattern:{0,}`. That is, the character may repeat any times or be absent.
82
+
: Significa "cero o más", igual que `pattern:{0,}`. Es decir, el carácter puede repetirse muchas veces o estar ausente.
83
83
84
-
For example, `pattern:\d0*` looks for a digit followed by any number of zeroes (may be many or none):
84
+
Por ejemplo, `pattern:\d0*` busca un dígito seguido de cualquier número de ceros (puede ser muchos o ninguno):
85
85
86
86
```js run
87
87
alert( "100 10 1".match(/\d0*/g) ); // 100, 10, 1
88
88
```
89
89
90
-
Compare it with `pattern:+` (one or more):
90
+
Compáralo con `pattern:+` (uno o más):
91
91
92
92
```js run
93
93
alert( "100 10 1".match(/\d0+/g) ); // 100, 10
94
-
// 1 not matched, as 0+ requires at least one zero
94
+
// 1 no coincide, ya que 0+ requiere al menos un cero
95
95
```
96
96
97
-
## More examples
97
+
## Más ejemplos
98
98
99
-
Quantifiers are used very often. They serve as the main "building block" of complex regular expressions, so let's see more examples.
99
+
Los cuantificadores se usan con mucha frecuencia. Sirven como el "bloque de construcción" principal de expresiones regulares complejas, así que veamos más ejemplos.
100
100
101
-
**Regexp for decimal fractions (a number with a floating point): `pattern:\d+\.\d+`**
101
+
**Regexp para fracciones decimales (un número con coma flotante): `pattern:\d+\.\d+`**
**Regexp "opening or closing HTML-tag without attributes":`pattern:/<\/?[a-z][a-z0-9]*>/i`**
126
+
**Regexp para "etiquetas HTML de apertura o cierre sin atributos":`pattern:/<\/?[a-z][a-z0-9]*>/i`**
127
127
128
-
We added an optional slash `pattern:/?`near the beginning of the pattern. Had to escape it with a backslash, otherwise JavaScript would think it is the pattern end.
128
+
Agregamos una barra opcional `pattern:/?`cerca del comienzo del patrón. Se tiene que escapar con una barra diagonal inversa, de lo contrario, JavaScript pensaría que es el final del patrón.
```smart header="To make a regexp more precise, we often need make it more complex"
135
-
We can see one common rule in these examples: the more precise is the regular expression -- the longer and more complex it is.
134
+
```smart header="Para hacer más precisa una regexp, a menudo necesitamos hacerla más compleja"
135
+
Podemos ver una regla común en estos ejemplos: cuanto más precisa es la expresión regular, es más larga y compleja.
136
136
137
-
For instance, for HTML tags we could use a simpler regexp: `pattern:<\w+>`. But as HTML has stricter restrictions for a tag name, `pattern:<[a-z][a-z0-9]*>`is more reliable.
137
+
Por ejemplo, para las etiquetas HTML debemos usar una regexp más simple: `pattern:<\w+>`. Pero como HTML tiene normas estrictas para los nombres de etiqueta, `pattern:<[a-z][a-z0-9]*>`es más confiable.
138
138
139
-
Can we use `pattern:<\w+>`or we need`pattern:<[a-z][a-z0-9]*>`?
In real life both variants are acceptable. Depends on how tolerant we can be to "extra" matches and whether it's difficult or not to remove them from the result by other means.
141
+
En la vida real, ambas variantes son aceptables. Depende de cuán tolerantes podamos ser a las coincidencias "adicionales" y si es difícil o no eliminarlas del resultado por otros medios.
0 commit comments