Skip to content

Commit 1909909

Browse files
committed
translated chapter 12, regexp anchors article
1 parent 8241bee commit 1909909

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed
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)