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