|
1 |
| -# Alternation (OR) | |
| 1 | +# Altarnancia (OR (O)) | |
2 | 2 |
|
3 |
| -Alternation is the term in regular expression that is actually a simple "OR". |
| 3 | +Alternancia es un término en expresión regular que simplemente significa "O". |
4 | 4 |
|
5 |
| -In a regular expression it is denoted with a vertical line character `pattern:|`. |
| 5 | +En una expresión regular se denota con un carácter de línea vertical `pattern:|`. |
6 | 6 |
|
7 |
| -For instance, we need to find programming languages: HTML, PHP, Java or JavaScript. |
| 7 | +Por ejemplo, necesitamos encontrar lenguajes de programación: HTML, PHP, Java o JavaScript. |
8 | 8 |
|
9 |
| -The corresponding regexp: `pattern:html|php|java(script)?`. |
| 9 | +La expresión regular correspondiente es: `pattern:html|php|java(script)?`. |
10 | 10 |
|
11 |
| -A usage example: |
| 11 | +Un ejemplo de uso: |
12 | 12 |
|
13 | 13 | ```js run
|
14 | 14 | let regexp = /html|php|css|java(script)?/gi;
|
15 | 15 |
|
16 |
| -let str = "First HTML appeared, then CSS, then JavaScript"; |
| 16 | +let str = "Primera aparición de HTML, luego CSS, luego JavaScript"; |
17 | 17 |
|
18 | 18 | alert( str.match(regexp) ); // 'HTML', 'CSS', 'JavaScript'
|
19 | 19 | ```
|
20 | 20 |
|
21 |
| -We already saw a similar thing -- square brackets. They allow to choose between multiple characters, for instance `pattern:gr[ae]y` matches `match:gray` or `match:grey`. |
| 21 | +Ya vimos algo similar: corchetes. Permiten elegir entre varios caracteres, por ejemplo `pattern:gr[ae]y` coincide con `match:gray` o `match:grey`. |
22 | 22 |
|
23 |
| -Square brackets allow only characters or character classes. Alternation allows any expressions. A regexp `pattern:A|B|C` means one of expressions `A`, `B` or `C`. |
| 23 | +Los corchetes solo permiten caracteres o conjuntos de caracteres. La alternancia permite cualquier expresión. Una expresión regular `pattern:A|B|C` significa una de las expresiones `A`, `B` o `C`. |
24 | 24 |
|
25 |
| -For instance: |
| 25 | +Por ejemplo: |
26 | 26 |
|
27 |
| -- `pattern:gr(a|e)y` means exactly the same as `pattern:gr[ae]y`. |
28 |
| -- `pattern:gra|ey` means `match:gra` or `match:ey`. |
| 27 | +- `pattern:gr(a|e)y` significa exactamente lo mismo que `pattern:gr[ae]y`. |
| 28 | +- `pattern:gra|ey` significa `match:gra` o `match:ey`. |
29 | 29 |
|
30 |
| -To apply alternation to a chosen part of the pattern, we can enclose it in parentheses: |
31 |
| -- `pattern:I love HTML|CSS` matches `match:I love HTML` or `match:CSS`. |
32 |
| -- `pattern:I love (HTML|CSS)` matches `match:I love HTML` or `match:I love CSS`. |
| 30 | +Para aplicar la alternancia a una parte elegida del patrón, podemos encerrarla entre paréntesis: |
| 31 | +- `pattern:I love HTML|CSS` coincide con `match:I love HTML` o `match:CSS`. |
| 32 | +- `pattern:I love (HTML|CSS)` coincide con `match:I love HTML` o `match:I love CSS`. |
33 | 33 |
|
34 |
| -## Example: regexp for time |
| 34 | +## Ejemplo: Expresión regular para el tiempo |
35 | 35 |
|
36 |
| -In previous articles there was a task to build a regexp for searching time in the form `hh:mm`, for instance `12:00`. But a simple `pattern:\d\d:\d\d` is too vague. It accepts `25:99` as the time (as 99 minutes match the pattern, but that time is invalid). |
| 36 | +En artículos anteriores había una tarea para construir una expresión regular para buscar un horario en la forma `hh:mm`, por ejemplo `12:00`. Pero esta simple expresión `pattern:\d\d:\d\d` es muy vaga. Acepta `25:99` como tiempo (ya que 99 segundos coinciden con el patrón, pero ese tiempo no es válido). |
37 | 37 |
|
38 |
| -How can we make a better pattern? |
| 38 | +¿Cómo podemos hacer un mejor patrón? |
39 | 39 |
|
40 |
| -We can use more careful matching. First, the hours: |
| 40 | +Podemos utilizar una combinación más cuidadosa. Primero, las horas: |
41 | 41 |
|
42 |
| -- If the first digit is `0` or `1`, then the next digit can be any: `pattern:[01]\d`. |
43 |
| -- Otherwise, if the first digit is `2`, then the next must be `pattern:[0-3]`. |
44 |
| -- (no other first digit is allowed) |
| 42 | +- Si el primer dígito es `0` o `1`, entonces el siguiente dígito puede ser cualquiera: `pattern:[01]\d`. |
| 43 | +- De otra manera, si el primer dígito es `2`, entonces el siguiente debe ser `pattern:[0-3]`. |
| 44 | +- (no se permite ningún otro dígito) |
45 | 45 |
|
46 |
| -We can write both variants in a regexp using alternation: `pattern:[01]\d|2[0-3]`. |
| 46 | +Podemos escribir ambas variantes en una expresión regular usando alternancia: `pattern:[01]\d|2[0-3]`. |
47 | 47 |
|
48 |
| -Next, minutes must be from `00` to `59`. In the regular expression language that can be written as `pattern:[0-5]\d`: the first digit `0-5`, and then any digit. |
| 48 | +A continuación, los minutos deben estar comprendidos entre `00` y `59`. En el lenguaje de expresiones regulares que se puede escribir como `pattern:[0-5]\d`: el primer dígito `0-5`, y luego culquier otro. |
49 | 49 |
|
50 |
| -If we glue hours and minutes together, we get the pattern: `pattern:[01]\d|2[0-3]:[0-5]\d`. |
| 50 | +Si pegamos minutos y segundos juntos, obtenemos el patrón: `pattern:[01]\d|2[0-3]:[0-5]\d`. |
51 | 51 |
|
52 |
| -We're almost done, but there's a problem. The alternation `pattern:|` now happens to be between `pattern:[01]\d` and `pattern:2[0-3]:[0-5]\d`. |
| 52 | +Ya casi terminamos, pero hay un problema. La alternancia `pattern:|` ahora pasa a estar entre `pattern:[01]\d` y `pattern:2[0-3]:[0-5]\d`. |
53 | 53 |
|
54 |
| -That is: minutes are added to the second alternation variant, here's a clear picture: |
| 54 | +Es decir: se agregan minutos a la segunda variante de alternancia, aquí hay una imagen clara: |
55 | 55 |
|
56 | 56 | ```
|
57 | 57 | [01]\d | 2[0-3]:[0-5]\d
|
58 | 58 | ```
|
59 | 59 |
|
60 |
| -That pattern looks for `pattern:[01]\d` or `pattern:2[0-3]:[0-5]\d`. |
| 60 | +Este patrón busca `pattern:[01]\d` o `pattern:2[0-3]:[0-5]\d`. |
61 | 61 |
|
62 |
| -But that's wrong, the alternation should only be used in the "hours" part of the regular expression, to allow `pattern:[01]\d` OR `pattern:2[0-3]`. Let's correct that by enclosing "hours" into parentheses: `pattern:([01]\d|2[0-3]):[0-5]\d`. |
| 62 | +Pero eso es incorrecto, la alternancia solo debe usarse en la parte "horas" de la expresión regular, para permitir `pattern:[01]\d` O `pattern:2[0-3]`. Corregiremos eso encerrando las "horas" entre paréntesis: `pattern:([01]\d|2[0-3]):[0-5]\d`. |
63 | 63 |
|
64 |
| -The final solution: |
| 64 | +La solución final sería: |
65 | 65 |
|
66 | 66 | ```js run
|
67 | 67 | let regexp = /([01]\d|2[0-3]):[0-5]\d/g;
|
|
0 commit comments