Skip to content

Commit b839ade

Browse files
authored
Merge pull request #48 from eadensplace/anchors-string-start-and-end
Anchors: string start ^ and end $
2 parents 19f5c20 + 399df24 commit b839ade

File tree

5 files changed

+37
-37
lines changed

5 files changed

+37
-37
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
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.
33

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.
55

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.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Regexp ^$
22

3-
Which string matches the pattern `pattern:^$`?
3+
Quale stringa corrisponde al pattern `pattern:^$`?
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
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).
22

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);
44

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}`
66

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:^...$`.
88

9-
Finally:
9+
Infine:
1010

1111
```js run
1212
let reg = /^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/i;
1313

1414
alert( reg.test('01:32:54:67:89:AB') ); // true
1515

16-
alert( reg.test('0132546789AB') ); // false (no colons)
16+
alert( reg.test('0132546789AB') ); // false (non ci sono i due punti)
1717

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)
1919

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)
2121
```
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# Check MAC-address
1+
# Controllo MAC-address
22

3-
[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.
44

5-
For instance: `subject:'01:32:54:67:89:AB'`.
5+
Per esempio: `subject:'01:32:54:67:89:AB'`.
66

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.
88

9-
Usage:
9+
Uso:
1010
```js
11-
let reg = /your regexp/;
11+
let reg = /la tua regexp/;
1212

1313
alert( reg.test('01:32:54:67:89:AB') ); // true
1414

15-
alert( reg.test('0132546789AB') ); // false (no colons)
15+
alert( reg.test('0132546789AB') ); // false (non ci sono i due punti)
1616

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)
1818

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)
2020
```
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# String start ^ and finish $
1+
# Inizio stringa ^ e fine $
22

3-
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).
44

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.
66

7-
For instance, let's test if the text starts with `Mary`:
7+
Per esempio, vediamo se il testo inizia con `Mary`:
88

99
```js run
1010
let str1 = "Mary had a little lamb, it's fleece was white as snow";
@@ -14,13 +14,13 @@ alert( /^Mary/.test(str1) ); // true
1414
alert( /^Mary/.test(str2) ); // false
1515
```
1616

17-
The pattern `pattern:^Mary` means: "the string start and then Mary".
17+
Il pattern `pattern:^Mary` vuol dire: "la stringa inizia e subito dopo c'è Mary".
1818

19-
Now let's test whether the text ends with an email.
19+
Ora verifichiamo se il testo finisce con una email.
2020

21-
To match an email, we can use a regexp `pattern:[-.\w]+@([\w-]+\.)+[\w-]{2,20}`.
21+
Per trovare corrispondenza con un'email, possiamo usare la regexp `pattern:[-.\w]+@([\w-]+\.)+[\w-]{2,20}`.
2222

23-
To test whether the string ends with the email, let's add `pattern:$` to the pattern:
23+
Per testare se la stringa finisca con una email, aggiungiamo `pattern:$` al pattern:
2424

2525
```js run
2626
let reg = /[-.\w]+@([\w-]+\.)+[\w-]{2,20}$/g;
@@ -32,24 +32,24 @@ alert( reg.test(str1) ); // true
3232
alert( reg.test(str2) ); // false
3333
```
3434

35-
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.
3636

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}`.
3838

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:^...$`:
4040

4141
```js run
4242
let str = "#abcdef";
4343

4444
alert( /^#[0-9a-f]{6}$/i.test(str) ); // true
4545
```
4646

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.
4848

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.
5151
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).
5353
```
5454

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

Comments
 (0)