-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from cortizg/es.javascript.info.9-06-wb
Word boundary: \b
- Loading branch information
Showing
3 changed files
with
28 additions
and
28 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
9-regular-expressions/06-regexp-boundary/1-find-time-hh-mm/solution.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
|
||
The answer: `pattern:\b\d\d:\d\d\b`. | ||
La respuesta: `pattern:\b\d\d:\d\d\b`. | ||
|
||
```js run | ||
alert( "Breakfast at 09:00 in the room 123:456.".match( /\b\d\d:\d\d\b/ ) ); // 09:00 | ||
alert( "Desayuno a las 09:00 en la habitación 123:456.".match( /\b\d\d:\d\d\b/ ) ); // 09:00 | ||
``` |
10 changes: 5 additions & 5 deletions
10
9-regular-expressions/06-regexp-boundary/1-find-time-hh-mm/task.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
# Find the time | ||
# Encuentra la hora | ||
|
||
The time has a format: `hours:minutes`. Both hours and minutes has two digits, like `09:00`. | ||
La hora tiene un formato: `horas:minutos`. Tanto las horas como los minutos tienen dos dígitos, como `09:00`. | ||
|
||
Make a regexp to find time in the string: `subject:Breakfast at 09:00 in the room 123:456.` | ||
Haz una expresión regular para encontrar el tiempo en la cadena: `subject:Desayuno a las 09:00 en la habitación 123:456.` | ||
|
||
P.S. In this task there's no need to check time correctness yet, so `25:99` can also be a valid result. | ||
P.D.: En esta tarea todavía no hay necesidad de verificar la corrección del tiempo, por lo que `25:99` también puede ser un resultado válido. | ||
|
||
P.P.S. The regexp shouldn't match `123:456`. | ||
P.P.D.: La expresión regular no debe coincidir con `123:456`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
# Word boundary: \b | ||
# Límite de palabra: \b | ||
|
||
A word boundary `pattern:\b` is a test, just like `pattern:^` and `pattern:$`. | ||
Un límite de palabra `pattern:\b` es una prueba, al igual que `pattern:^` y `pattter:$`. | ||
|
||
When the regexp engine (program module that implements searching for regexps) comes across `pattern:\b`, it checks that the position in the string is a word boundary. | ||
Cuando el motor regex (módulo de programa que implementa la búsqueda de expresiones regulares) se encuentra con `pattern:\b`, comprueba que la posición en la cadena es un límite de palabra. | ||
|
||
There are three different positions that qualify as word boundaries: | ||
Hay tres posiciones diferentes que califican como límites de palabras: | ||
|
||
- At string start, if the first string character is a word character `pattern:\w`. | ||
- Between two characters in the string, where one is a word character `pattern:\w` and the other is not. | ||
- At string end, if the last string character is a word character `pattern:\w`. | ||
- Al comienzo de la cadena, si el primer carácter de cadena es un carácter de palabra `pattern:\w`. | ||
- Entre dos caracteres en la cadena, donde uno es un carácter de palabra `pattern:\w` y el otro no. | ||
- Al final de la cadena, si el último carácter de la cadena es un carácter de palabra `pattern:\w`. | ||
|
||
For instance, regexp `pattern:\bJava\b` will be found in `subject:Hello, Java!`, where `subject:Java` is a standalone word, but not in `subject:Hello, JavaScript!`. | ||
Por ejemplo, la expresión regular `pattern:\bJava\b` se encontrará en `subject:Hello, Java!`, donde `subject:Java` es una palabra independiente, pero no en `subject:Hello, JavaScript!`. | ||
|
||
```js run | ||
alert( "Hello, Java!".match(/\bJava\b/) ); // Java | ||
alert( "Hello, JavaScript!".match(/\bJava\b/) ); // null | ||
``` | ||
|
||
In the string `subject:Hello, Java!` following positions correspond to `pattern:\b`: | ||
En la cadena `subject:Hello, Java!` las flechas que se muestran corresponden a `pattern:\b`, ver imagen: | ||
|
||
![](hello-java-boundaries.svg) | ||
|
||
So, it matches the pattern `pattern:\bHello\b`, because: | ||
Entonces, coincide con el patrón `pattern:\bHello\b`, porque: | ||
|
||
1. At the beginning of the string matches the first test `pattern:\b`. | ||
2. Then matches the word `pattern:Hello`. | ||
3. Then the test `pattern:\b` matches again, as we're between `subject:o` and a comma. | ||
1. Al comienzo de la cadena coincide con la primera prueba: `pattern:\b`. | ||
2. Luego coincide con la palabra `pattern:Hello`. | ||
3. Luego, la prueba `pattern:\b` vuelve a coincidir, ya que estamos entre `subject:o` y una coma. | ||
|
||
The pattern `pattern:\bHello\b` would also match. But not `pattern:\bHell\b` (because there's no word boundary after `l`) and not `Java!\b` (because the exclamation sign is not a wordly character `pattern:\w`, so there's no word boundary after it). | ||
El patrón `pattern:\bHello\b` también coincidiría. Pero no `pattern:\bHel\b` (porque no hay límite de palabras después de `l`) y tampoco `Java!\b` (porque el signo de exclamación no es un carácter común `pattern:\w`, entonces no hay límite de palabras después de eso). | ||
|
||
```js run | ||
alert( "Hello, Java!".match(/\bHello\b/) ); // Hello | ||
alert( "Hello, Java!".match(/\bJava\b/) ); // Java | ||
alert( "Hello, Java!".match(/\bHell\b/) ); // null (no match) | ||
alert( "Hello, Java!".match(/\bJava!\b/) ); // null (no match) | ||
alert( "Hello, Java!".match(/\bHell\b/) ); // null (sin coincidencia) | ||
alert( "Hello, Java!".match(/\bJava!\b/) ); // null (sin coincidencia) | ||
``` | ||
|
||
We can use `pattern:\b` not only with words, but with digits as well. | ||
Podemos usar `pattern:\b` no solo con palabras, sino también con dígitos. | ||
|
||
For example, the pattern `pattern:\b\d\d\b` looks for standalone 2-digit numbers. In other words, it looks for 2-digit numbers that are surrounded by characters different from `pattern:\w`, such as spaces or punctuation (or text start/end). | ||
Por ejemplo, el patrón `pattern:\b\d\d\b` busca números independientes de 2 dígitos. En otras palabras, busca números de 2 dígitos que están rodeados por caracteres diferentes de `pattern:\w`, como espacios o signos de puntuación (o texto de inicio/fin). | ||
|
||
```js run | ||
alert( "1 23 456 78".match(/\b\d\d\b/g) ); // 23,78 | ||
alert( "12,34,56".match(/\b\d\d\b/g) ); // 12,34,56 | ||
``` | ||
|
||
```warn header="Word boundary `pattern:\b` doesn't work for non-latin alphabets" | ||
The word boundary test `pattern:\b` checks that there should be `pattern:\w` on the one side from the position and "not `pattern:\w`" - on the other side. | ||
```warn header="El límite de palabra `pattern:\b` no funciona para alfabetos no latinos" | ||
La prueba de límite de palabra `pattern:\b` verifica que debe haber un `pattern:\w` en un lado de la posición y "no `pattern:\w`"- en el otro lado. | ||
|
||
But `pattern:\w` means a latin letter `a-z` (or a digit or an underscore), so the test doesn't work for other characters, e.g. cyrillic letters or hieroglyphs. | ||
Pero `pattern:\w` significa una letra latina `a-z` (o un dígito o un guión bajo), por lo que la prueba no funciona para otros caracteres, p.ej.: letras cirílicas o jeroglíficos. | ||
``` |