Skip to content

Commit 6324b44

Browse files
authored
Merge pull request #269 from cortizg/es.javascript.info.9-05-mma
Multiline mode of anchors ^ $, flag "m"
2 parents c91572b + 3fc2778 commit 6324b44

File tree

1 file changed

+30
-30
lines changed
  • 9-regular-expressions/05-regexp-multiline-mode

1 file changed

+30
-30
lines changed
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
# Multiline mode of anchors ^ $, flag "m"
1+
# Modo multilínea de anclas ^ $, bandera "m"
22

3-
The multiline mode is enabled by the flag `pattern:m`.
3+
El modo multilínea está habilitado por el indicador `pattern:m`.
44

5-
It only affects the behavior of `pattern:^` and `pattern:$`.
5+
Solo afecta el comportamiento de `pattern:^` y `pattern:$`.
66

7-
In the multiline mode they match not only at the beginning and the end of the string, but also at start/end of line.
7+
En el modo multilínea, coinciden no solo al principio y al final de la cadena, sino también al inicio/final de la línea.
88

9-
## Searching at line start ^
9+
## Buscando al inicio de línea ^
1010

11-
In the example below the text has multiple lines. The pattern `pattern:/^\d/gm` takes a digit from the beginning of each line:
11+
En el siguiente ejemplo, el texto tiene varias líneas. El patrón `pattern:/^\d/gm` toma un dígito desde el principio de cada línea:
1212

1313
```js run
14-
let str = `1st place: Winnie
15-
2nd place: Piglet
16-
3rd place: Eeyore`;
14+
let str = `1er lugar: Winnie
15+
2do lugar: Piglet
16+
3er lugar: Eeyore`;
1717

1818
*!*
1919
alert( str.match(/^\d/gm) ); // 1, 2, 3
2020
*/!*
2121
```
2222

23-
Without the flag `pattern:m` only the first digit is matched:
23+
Sin la bandera `pattern:m` solo coincide el primer dígito:
2424

2525
```js run
26-
let str = `1st place: Winnie
27-
2nd place: Piglet
28-
3rd place: Eeyore`;
26+
let str = `1er lugar: Winnie
27+
2do lugar: Piglet
28+
3er lugar: Eeyore`;
2929

3030
*!*
3131
alert( str.match(/^\d/g) ); // 1
3232
*/!*
3333
```
3434

35-
That's because by default a caret `pattern:^` only matches at the beginning of the text, and in the multiline mode -- at the start of any line.
35+
Esto se debe a que, de forma predeterminada, un caret `pattern:^` solo coincide al inicio del texto y en el modo multilínea, al inicio de cualquier línea.
3636

3737
```smart
38-
"Start of a line" formally means "immediately after a line break": the test `pattern:^` in multiline mode matches at all positions preceeded by a newline character `\n`.
38+
"Inicio de una línea" significa formalmente "inmediatamente después de un salto de línea": la prueba `pattern:^` en modo multilínea coincide en todas las posiciones precedidas por un carácter de línea nueva `\n`.
3939
40-
And at the text start.
40+
Y al comienzo del texto.
4141
```
4242

43-
## Searching at line end $
43+
## Buscando al final de la línea $
4444

45-
The dollar sign `pattern:$` behaves similarly.
45+
El signo de dólar `pattern:$` se comporta de manera similar.
4646

47-
The regular expression `pattern:\d$` finds the last digit in every line
47+
La expresión regular `pattern:\d$` encuentra el último dígito en cada línea
4848

4949
```js run
5050
let str = `Winnie: 1
@@ -54,21 +54,21 @@ Eeyore: 3`;
5454
alert( str.match(/\d$/gm) ); // 1,2,3
5555
```
5656

57-
Without the flag `pattern:m`, the dollar `pattern:$` would only match the end of the whole text, so only the very last digit would be found.
57+
Sin la bandera `pattern:m`, dólar `pattern:$` solo coincidiría con el final del texto completo, por lo que solo se encontraría el último dígito.
5858

5959
```smart
60-
"End of a line" formally means "immediately before a line break": the test `pattern:$` in multiline mode matches at all positions succeeded by a newline character `\n`.
60+
"Fin de una línea" significa formalmente "inmediatamente antes de un salto de línea": la prueba `pattern:$` en el modo multilínea coincide en todas las posiciones seguidas por un carácter de línea nueva `\n`.
6161
62-
And at the text end.
62+
Y al final del texto.
6363
```
6464

65-
## Searching for \n instead of ^ $
65+
## Buscando \n en lugar de ^ $
6666

67-
To find a newline, we can use not only anchors `pattern:^` and `pattern:$`, but also the newline character `\n`.
67+
Para encontrar una línea nueva, podemos usar no solo las anclas `pattern:^` y `pattern:$`, sino también el carácter de línea nueva `\n`.
6868

69-
What's the difference? Let's see an example.
69+
¿Cual es la diferencia? Veamos un ejemplo.
7070

71-
Here we search for `pattern:\d\n` instead of `pattern:\d$`:
71+
Buscamos `pattern:\d\n` en lugar de `pattern:\d$`:
7272

7373
```js run
7474
let str = `Winnie: 1
@@ -78,10 +78,10 @@ Eeyore: 3`;
7878
alert( str.match(/\d\n/gm) ); // 1\n,2\n
7979
```
8080

81-
As we can see, there are 2 matches instead of 3.
81+
Como podemos ver, hay 2 coincidencias en lugar de 3.
8282

83-
That's because there's no newline after `subject:3` (there's text end though, so it matches `pattern:$`).
83+
Esto se debe a que no hay una línea nueva después de `subject:3` (sin embargo, hay un final de texto, por lo que coincide con `pattern:$`).
8484

85-
Another difference: now every match includes a newline character `match:\n`. Unlike the anchors `pattern:^` `pattern:$`, that only test the condition (start/end of a line), `\n` is a character, so it becomes a part of the result.
85+
Otra diferencia: ahora cada coincidencia incluye un carácter de línea nueva `match:\n`. A diferencia de las anclas `pattern:^` `pattern:$`, que solo prueban la condición (inicio/final de una línea), `\n` es un carácter, por lo que se hace parte del resultado.
8686

87-
So, a `\n` in the pattern is used when we need newline characters in the result, while anchors are used to find something at the beginning/end of a line.
87+
Entonces, un `\n` en el patrón se usa cuando necesitamos encontrar caracteres de línea nueva, mientras que las anclas se usan para encontrar algo "al principio/al final" de una línea.

0 commit comments

Comments
 (0)