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
Se usiamo `for..of`to loop over le corrispondenze di `matchAll`, non necessitiamo più di `Array.from`.
89
+
Se usiamo `for..of`per accedere alle corrispondenze di `matchAll`, non necessitiamo più di `Array.from`.
90
90
91
91
## str.split(regexp|substr, limit)
92
92
@@ -254,14 +254,14 @@ Si comporta differentemente a seconda della presenza o meno della flag `pattern:
254
254
Se non c'è `pattern:g`, `regexp.exec(str)` restituisce la prima corrispondenza, esattamente come `str.match(regexp)`. Questo comportamento non comporta niente di nuovo.
255
255
256
256
Ma se c'è una flag `pattern:g`, allora:
257
-
-A call to`regexp.exec(str)`returns the first match and saves the position immediately after it in the property`regexp.lastIndex`.
258
-
-The next such call starts the search from position `regexp.lastIndex`, returns the next match and saves the position after it in `regexp.lastIndex`.
259
-
- ...And so on.
260
-
-If there are no matches, `regexp.exec`returns`null`and resets`regexp.lastIndex`to`0`.
257
+
-Una chiamata a`regexp.exec(str)`restituisce la prima corrispondenza e salva la posizione immediatamente successiva a essa nella proprietà`regexp.lastIndex`.
258
+
-La successiva chiamata di tale tipo inizia la ricerca a partire dalla posizione `regexp.lastIndex`, restituisce la corrispondenza successiva e salva la posizione seguente in `regexp.lastIndex`.
259
+
- ...E così via.
260
+
-Se non esistono corrispondenze, `regexp.exec`restituisce`null`e reimposta`regexp.lastIndex`a`0`.
261
261
262
-
So, repeated calls return all matches one after another, using property `regexp.lastIndex`to keep track of the current search position.
262
+
Perciò, chiamate ripetute restituiscono tutte le corrispondenze una dopo l'altra, usando la proprietà `regexp.lastIndex`per tenere traccia della posizione di ricerca corrente.
263
263
264
-
In the past, before the method `str.matchAll`was added to JavaScript, calls of `regexp.exec`were used in the loop to get all matches with groups:
264
+
Nel passato, prima che il metodo `str.matchAll`fosse implementato in JavaScript, le chiamate a `regexp.exec`erano usate nel loop per ottenere tutte le corrispondenze con i gruppi:
265
265
266
266
```js run
267
267
let str ='More about JavaScript at https://javascript.info';
@@ -271,56 +271,56 @@ let result;
271
271
272
272
while (result =regexp.exec(str)) {
273
273
alert( `Found ${result[0]} at position ${result.index}` );
274
-
//Found JavaScript at position 11, then
275
-
//Found javascript at position 33
274
+
// JavaScript trovato alla posizione 11, quindi
275
+
// javascript trovato alla posizione 33
276
276
}
277
277
```
278
278
279
-
This works now as well, although for newer browsers `str.matchAll`is usually more convenient.
279
+
Questo funziona anche adesso, anche se per i browser moderni solitamente `str.matchAll`è più conveniente.
280
280
281
-
**We can use `regexp.exec`to search from a given position by manually setting`lastIndex`.**
281
+
**Possiamo usare `regexp.exec`per cercare a partire da una posizione data impostando manualmente`lastIndex`.**
282
282
283
-
For instance:
283
+
Per esempio:
284
284
285
285
```js run
286
286
let str ='Hello, world!';
287
287
288
-
let regexp =/\w+/g; //without flag "g", lastIndex property is ignored
289
-
regexp.lastIndex=5; //search from 5th position (from the comma)
288
+
let regexp =/\w+/g; //senza la flag "g", la proprietà lastIndex viene ignorata
289
+
regexp.lastIndex=5; //ricerca a partire dalla quinta posizione (dalla virgola)
290
290
291
291
alert( regexp.exec(str) ); // world
292
292
```
293
293
294
-
If the regexp has flag `pattern:y`, then the search will be performed exactly at the position `regexp.lastIndex`, not any further.
294
+
Se l'espressione regolare presenta la flag `pattern:y`, la ricerca sarà eseguita esattamente alla posizione `regexp.lastIndex`, senza andare oltre.
295
295
296
-
Let's replace flag `pattern:g`with`pattern:y`in the example above. There will be no matches, as there's no word at position`5`:
296
+
Sostituiamo la flag `pattern:g`con`pattern:y`nell'esempio sopra. Non ci saranno corrispondenze, in quanto non c'è una parola alla posizione`5`:
297
297
298
298
```js run
299
299
let str ='Hello, world!';
300
300
301
301
let regexp =/\w+/y;
302
-
regexp.lastIndex=5; //search exactly at position 5
302
+
regexp.lastIndex=5; //ricerca esattamente alla posizione 5
303
303
304
304
alert( regexp.exec(str) ); // null
305
305
```
306
306
307
-
That's convenient for situations when we need to "read" something from the string by a regexp at the exact position, not somewhere further.
307
+
Ciò è conveniente per le situazioni in cui abbiamo bisogno di "leggere" qualcosa dalla stringa con un'espressione regolare alla posizione esatta, non da qualche parte più lontano.
308
308
309
309
## regexp.test(str)
310
310
311
-
The method`regexp.test(str)`looks for a match and returns `true/false`whether it exists.
311
+
Il metodo`regexp.test(str)`cerca una corrispondenza e restituisce `true/false`a seconda della sua esistenza.
````warn header="Same global regexp tested repeatedly on different sources may fail"
347
-
If we apply the same global regexp to different inputs, it may lead to wrong result, because `regexp.test`call advances `regexp.lastIndex` property, so the search in another string may start from non-zero position.
347
+
Se applichiamo la stessa espressione regolare globale a input differenti, potrebbe produrre un risultato sbagliato, poiché la chiamata di `regexp.test`manda avanti la proprietà `regexp.lastIndex`, pertanto la ricerca in un'altra stringa potrebbe iniziare da una posizione diversa da 0.
348
348
349
-
For instance, here we call `regexp.test`twice on the same text, and the second time fails:
349
+
Per esempio, qui chiamiamo `regexp.test`due volte sullo stesso testo, e la seconda volta fallisce:
350
350
351
351
```js run
352
-
let regexp =/javascript/g; // (regexp just created: regexp.lastIndex=0)
352
+
let regexp =/javascript/g; // (espressione regolare appena creata: regexp.lastIndex=0)
That's exactly because `regexp.lastIndex`is non-zero in the second test.
358
+
Ciò avviene esattamente perché `regexp.lastIndex`è diverso da 0 nella seconda prova.
359
359
360
-
To work around that, we can set `regexp.lastIndex=0`before each search. Or instead of calling methods on regexp, use string methods `str.match/search/...`, they don't use`lastIndex`.
360
+
Per risolvere, possiamo impostare `regexp.lastIndex=0`prima di ogni ricerca. O invece di chiamare metodi sull'espressione regolare, usare i metodi delle stringhe `str.match/search/...`; essi non usano`lastIndex`.
0 commit comments