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
Copy file name to clipboardExpand all lines: 9-regular-expressions/10-regexp-greedy-and-lazy/article.md
+14-14Lines changed: 14 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,20 @@
1
-
# Greedy and lazy quantifiers
1
+
# Modalità greedy e lazy dei quantificatori
2
2
3
-
Quantifiers are very simple from the first sight, but in fact they can be tricky.
3
+
I quantificatori sono molto semplici a prima vista, ma in realtà possono essere complicati.
4
4
5
-
We should understand how the search works very well if we plan to look for something more complex than`pattern:/\d+/`.
5
+
Dovremmo comprendere appieno come funziona la ricerca se intendiamo cercare qualcosa di più complesso di`pattern:/\d+/`.
6
6
7
-
Let's take the following task as an example.
7
+
Prendiamo ad esempio la seguente attività.
8
8
9
-
We have a text and need to replace all quotes `"..."`with guillemet marks: `«...»`. They are preferred for typography in many countries.
9
+
Abbiamo bisogno di rimpiazzare tutti i doppi apici `"..."`in un testo con le virgolette basse: `«...»`, che sono preferite nella tipografia di molti paesi.
10
10
11
-
For instance: `"Hello, world"`should become`«Hello, world»`. There exist other quotes, such as `„Witam, świat!”` (Polish) or`「你好,世界」` (Chinese), but for our task let's choose`«...»`.
11
+
Ad esempio: `"Hello, world"`dovrebbe diventare`«Hello, world»`. Esistono altre virgolette, come `„Witam, świat!”` (in Polonia) o`「你好,世界」` (in Cina). In questo caso, tuttavia, scegliamo`«...»`.
12
12
13
-
The first thing to do is to locate quoted strings, and then we can replace them.
13
+
Innanzitutto dobbiamo individuare le stringhe tra doppi apici per poi sostituirli.
14
14
15
-
A regular expression like `pattern:/".+"/g` (a quote, then something, then the other quote) may seem like a good fit, but it isn't!
15
+
Un'espressione regolare come `pattern:/".+"/g` (una stringa di lunghezza variabile racchiusa da doppi apici) può sembrare efficace, ma non lo è!
16
16
17
-
Let's try it:
17
+
Verifichiamo:
18
18
19
19
```js run
20
20
let regexp =/".+"/g;
@@ -24,13 +24,13 @@ let str = 'a "witch" and her "broom" is one';
24
24
alert( str.match(regexp) ); // "witch" and her "broom"
25
25
```
26
26
27
-
...We can see that it works not as intended!
27
+
...Non funziona come desideravamo!
28
28
29
-
Instead of finding two matches `match:"witch"`and`match:"broom"`, it finds one: `match:"witch" and her "broom"`.
29
+
Invece di trovare i due riscontri `match:"witch"`e`match:"broom"`, ne trova solo uno: `match:"witch" and her "broom"`.
30
30
31
-
That can be described as "greediness is the cause of all evil".
31
+
Questo fenomeno può essere descritto così: "l'avidità è la causa di tutti i mali".
32
32
33
-
## Greedy search
33
+
## La ricerca in modalità greedy (avida)
34
34
35
35
To find a match, the regular expression engine uses the following algorithm:
0 commit comments