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
The empty string is the only match: it starts and immediately finishes.
2
+
L'unica corrispondenza si ha con la stringa vuota: inizia e poi finisce immediatamente.
3
3
4
-
The task once again demonstrates that anchors are not characters, but tests.
4
+
Questo task dimostra ancora che gli ancoraggi non rappresentano caratteri, bensì test.
5
5
6
-
The string is empty`""`. The engine first matches the `pattern:^` (input start), yes it's there, and then immediately the end `pattern:$`, it's here too. So there's a match.
6
+
La stringa è vuota`""`. Il motore prima fa cerca corrispondenze per `pattern:^` (inizio input), ed è presente, e subito dopo cerca la fine `pattern:$`, e c'è anch'essa. Quindi c'è corrispondenza.
A two-digit hex number is `pattern:[0-9a-f]{2}` (assuming the `pattern:i`flag is enabled).
1
+
Una coppia di cifre esadecimali è `pattern:[0-9a-f]{2}` (assumendo che la flag `pattern:i`sia abilitata).
2
2
3
-
We need that number `NN`, and then`:NN`repeated 5 times (more numbers);
3
+
Abbiamo bisogno di quella coppia `NN`, e dopo`:NN`ripetuto per 5 volte (altre coppie);
4
4
5
-
The regexp is: `pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}`
5
+
La regexp è: `pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}`
6
6
7
-
Now let's show that the match should capture all the text: start at the beginning and end at the end. That's done by wrapping the pattern in`pattern:^...$`.
7
+
Ora dimostriamo che la corrispondenza catturi tutto il testo: che inizi con l'indirizzo MAC e finisca al suo termine. Otteniamo questo risultato circondando il pattern da`pattern:^...$`.
8
8
9
-
Finally:
9
+
Infine:
10
10
11
11
```js run
12
12
let reg =/^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/i;
13
13
14
14
alert( reg.test('01:32:54:67:89:AB') ); // true
15
15
16
-
alert( reg.test('0132546789AB') ); // false (no colons)
16
+
alert( reg.test('0132546789AB') ); // false (non ci sono i due punti)
17
17
18
-
alert( reg.test('01:32:54:67:89') ); // false (5 numbers, need 6)
18
+
alert( reg.test('01:32:54:67:89') ); // false (5 numeri, devono essere 6)
19
19
20
-
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ in the end)
20
+
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ alla fine)
[MAC-address](https://en.wikipedia.org/wiki/MAC_address) of a network interface consists of 6 two-digit hex numbers separated by a colon.
3
+
Il [MAC-address](https://it.wikipedia.org/wiki/Indirizzo_MAC) di un'interfaccia di rete è composto da 6 coppie di cifre esadecimali separati dai due punti.
4
4
5
-
For instance: `subject:'01:32:54:67:89:AB'`.
5
+
Per esempio: `subject:'01:32:54:67:89:AB'`.
6
6
7
-
Write a regexp that checks whether a string is MAC-address.
7
+
Scrivi una regexp che controlli se una stringa sia un MAC-address.
8
8
9
-
Usage:
9
+
Uso:
10
10
```js
11
-
let reg =/your regexp/;
11
+
let reg =/la tua regexp/;
12
12
13
13
alert( reg.test('01:32:54:67:89:AB') ); // true
14
14
15
-
alert( reg.test('0132546789AB') ); // false (no colons)
15
+
alert( reg.test('0132546789AB') ); // false (non ci sono i due punti)
16
16
17
-
alert( reg.test('01:32:54:67:89') ); // false (5 numbers, must be 6)
17
+
alert( reg.test('01:32:54:67:89') ); // false (5 coppie, devono essere 6)
18
18
19
-
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ ad the end)
19
+
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ alla fine)
The caret `pattern:'^'`and dollar `pattern:'$'`characters have special meaning in a regexp. They are called "anchors".
3
+
L'accento circonflesso `pattern:'^'`e il simbolo del dollaro `pattern:'$'`sono caratteri che hanno un significato speciale nelle regexp. Vengono chiamati "ancoraggi" (anchor).
4
4
5
-
The caret`pattern:^`matches at the beginning of the text, and the dollar`pattern:$`-- in the end.
5
+
Il simbolo`pattern:^`trova corrispondenza all'inizio del testo, e il dollaro`pattern:$`la trova alla fine del testo.
6
6
7
-
For instance, let's test if the text starts with`Mary`:
7
+
Per esempio, vediamo se il testo inizia con`Mary`:
8
8
9
9
```js run
10
10
let str1 ="Mary had a little lamb, it's fleece was white as snow";
We can use both anchors together to check whether the string exactly follows the pattern. That's often used for validation.
35
+
Possiamo utilizzare entrambi gli ancoraggi insieme per controllare che la stringa segua uno specifico pattern. È un metodo usato spesso per la validazione.
36
36
37
-
For instance we want to check that `str`is exactly a color in the form `#`plus 6 hex digits. The pattern for the color is`pattern:#[0-9a-f]{6}`.
37
+
Per esempio vogliamo controllare che `str`sia esattamente un colore nella forma `#`più 6 esadecimali. Il pattern per il colore è`pattern:#[0-9a-f]{6}`.
38
38
39
-
To check that the *whole string* exactly matches it, we add`pattern:^...$`:
39
+
Per verificare che l'*intera stringa* vi corrisponda in modo esatto, aggiungiamo`pattern:^...$`:
40
40
41
41
```js run
42
42
let str ="#abcdef";
43
43
44
44
alert(/^#[0-9a-f]{6}$/i.test(str) ); // true
45
45
```
46
46
47
-
The regexp engine looks for the text start, then the color, and then immediately the text end. Just what we need.
47
+
Il motore delle regexp cerca l'inizio del testo, successivamente il colore, e infine cerca immediatamente la fine del testo. Proprio ciò di cui abbiamo bisogno.
48
48
49
-
```smart header="Anchors have zero length"
50
-
Anchors just like `\b` are tests. They have zero-width.
49
+
```smart header="Gli ancoraggi hanno lunghezza zero"
50
+
Gli ancoraggi, proprio come `\b`, sono test. Hanno larghezza zero.
51
51
52
-
In other words, they do not match a character, but rather force the regexp engine to check the condition (text start/end).
52
+
In altre parole, non cercano corrispondenze per un carattere, piuttosto forzano il motore delle regexp a cercare la condizione specifica (inizio/fine del testo).
53
53
```
54
54
55
-
The behavior of anchors changes if there's a flag `pattern:m` (multiline mode). We'll explore it in the next chapter.
55
+
Il comportamento degli ancoraggi cambia se c'è la flag `pattern:m` (modalità multi linea). L'approfondiremo meglio nel prossimo capitolo.
0 commit comments