Skip to content

Commit b4a8450

Browse files
committed
Article translated
1 parent 600d5fd commit b4a8450

File tree

1 file changed

+33
-35
lines changed
  • 9-regular-expressions/13-regexp-multiline-mode

1 file changed

+33
-35
lines changed
Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,73 @@
1-
# Multiline mode, flag "m"
1+
# Modo multilinea, flag "m"
2+
Il modo multilinea si abilita col flag `pattern:/.../m`.
23

3-
The multiline mode is enabled by the flag `pattern:/.../m`.
4+
Esso modifica il comportamento dei soli `pattern:^` e `pattern:$`.
45

5-
It only affects the behavior of `pattern:^` and `pattern:$`.
6+
In modalità multilinea essi non trovano corrispondenza solo con l'inizio e la fine della stringa ma anche con gli inizi e fine di linea.
67

7-
In the multiline mode they match not only at the beginning and end of the string, but also at start/end of line.
8+
## Inizio linea ^
89

9-
## Line start ^
10-
11-
In the example below the text has multiple lines. The pattern `pattern:/^\d+/gm` takes a number from the beginning of each one:
10+
Nell'esempio sotto riportato il testo è distribuito su più linee. Il pattern `pattern:/^\d+/gm` prende un numero dall'inizio di ogni riga:
1211

1312
```js run
14-
let str = `1st place: Winnie
15-
2nd place: Piglet
16-
33rd place: Eeyore`;
13+
let str = `1o posto: Winnie
14+
2o posto: Piglet
15+
33o posto: Eeyore`;
1716

1817
*!*
1918
alert( str.match(/^\d+/gm) ); // 1, 2, 33
2019
*/!*
2120
```
2221

23-
The regexp engine moves along the text and looks for a line start `pattern:^`, when finds -- continues to match the rest of the pattern `pattern:\d+`.
24-
25-
Without the flag `pattern:/.../m` only the first number is matched:
22+
Il motore di regex si sposta sul testo alla ricerca di un inizio riga `pattern:^`, quando lo trova -- continua a trovare la corrispondenza col resto del pattern `pattern:\d+`.
23+
Mentre senza il flag `pattern:/.../m` troverebbe corrispondenza solo col primo numero:
2624

2725
```js run
28-
let str = `1st place: Winnie
29-
2nd place: Piglet
30-
33rd place: Eeyore`;
26+
let str = `1o posto: Winnie
27+
2o posto: Piglet
28+
33o posto: Eeyore`;
3129

3230
*!*
3331
alert( str.match(/^\d+/g) ); // 1
3432
*/!*
3533
```
3634

37-
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+
Questo accade perchè di base il comportamento del caret `pattern:^` trova corrispondenza solo con l'inizio del testo, mentre in modo multilinea trova -- con l'inizio di ogni riga.
3836

39-
## Line end $
37+
## Fine linea $
4038

41-
The dollar sign `pattern:$` behaves similarly.
39+
Il simbolo di dollaro `pattern:$` si comporta in modo simile.
4240

43-
The regular expression `pattern:\w+$` finds the last word in every line
41+
L'espressione regolare `pattern:\w+$` trova l'ultima parola in ogni stringa.
4442

4543
```js run
46-
let str = `1st place: Winnie
47-
2nd place: Piglet
48-
33rd place: Eeyore`;
44+
let str = `1o posto: Winnie
45+
2o posto: Piglet
46+
33o posto: Eeyore`;
4947

5048
alert( str.match(/\w+$/gim) ); // Winnie,Piglet,Eeyore
5149
```
5250

53-
Without the `pattern:/.../m` flag the dollar `pattern:$` would only match the end of the whole string, so only the very last word would be found.
51+
Senza il flag `pattern:/.../m` il dollaro `pattern:$` troverebbe corrispondenza solo con la fine dell'intera stringa, dunque risulterebbe solo l'ultima parola dell'intero testo.
5452

55-
## Anchors ^$ versus \n
53+
## Ancore ^$ rispetto \n (a-capo)
5654

57-
To find a newline, we can use not only `pattern:^` and `pattern:$`, but also the newline character `\n`.
55+
Per trovare un fine linea è possibile usare non solo `pattern:^` e `pattern:$`, ma anche il carattere a-capo `\n`.
5856

59-
The first difference is that unlike anchors, the character `\n` "consumes" the newline character and adds it to the result.
57+
La prima differenza è che, contrariamente alle ancore, il carattere `\n` "consuma" il carattere a-capo aggiungendolo al risultato.
6058

61-
For instance, here we use it instead of `pattern:$`:
59+
Ad esempio qui lo usiamo al posto di `pattern:$`:
6260

6361
```js run
64-
let str = `1st place: Winnie
65-
2nd place: Piglet
66-
33rd place: Eeyore`;
67-
62+
let str = `1o posto: Winnie
63+
2o posto: Piglet
64+
33o posto: Eeyore`;
6865
alert( str.match(/\w+\n/gim) ); // Winnie\n,Piglet\n
6966
```
7067

71-
Here every match is a word plus a newline character.
68+
Qui ogni corrispondenza è una parola più il carattere a-capo.
69+
70+
Un'altra differenza -- l' a-capo `\n` non trova corrispondenza a fine stringa. Questo è il motivo per cui `Eeyore` non è trovato nell'esempio sopra.
7271

73-
And one more difference -- the newline `\n` does not match at the string end. That's why `Eeyore` is not found in the example above.
72+
Pertanto le ancore sono migliori: sono più vicine a ciò che si vuole estrarre.
7473

75-
So, anchors are usually better, they are closer to what we want to get.

0 commit comments

Comments
 (0)