Skip to content

Commit fe89021

Browse files
authored
Traduzione completa
Traduzione completa
1 parent 472a5f0 commit fe89021

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

9-regular-expressions/17-regexp-methods/article.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ alert( firstMatch.index ); // 0
8686
alert( firstMatch.input ); // <h1>Hello, world!</h1>
8787
```
8888

89-
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`.
9090

9191
## str.split(regexp|substr, limit)
9292

@@ -254,14 +254,14 @@ Si comporta differentemente a seconda della presenza o meno della flag `pattern:
254254
Se non c'è `pattern:g`, `regexp.exec(str)` restituisce la prima corrispondenza, esattamente come `str.match(regexp)`. Questo comportamento non comporta niente di nuovo.
255255

256256
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`.
261261

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

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

266266
```js run
267267
let str = 'More about JavaScript at https://javascript.info';
@@ -271,56 +271,56 @@ let result;
271271

272272
while (result = regexp.exec(str)) {
273273
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
276276
}
277277
```
278278

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

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`.**
282282

283-
For instance:
283+
Per esempio:
284284

285285
```js run
286286
let str = 'Hello, world!';
287287

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

291291
alert( regexp.exec(str) ); // world
292292
```
293293

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

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`:
297297

298298
```js run
299299
let str = 'Hello, world!';
300300

301301
let regexp = /\w+/y;
302-
regexp.lastIndex = 5; // search exactly at position 5
302+
regexp.lastIndex = 5; // ricerca esattamente alla posizione 5
303303

304304
alert( regexp.exec(str) ); // null
305305
```
306306

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

309309
## regexp.test(str)
310310

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

313-
For instance:
313+
Per esempio:
314314

315315
```js run
316316
let str = "I love JavaScript";
317317

318-
// these two tests do the same
318+
// questi due esempi fanno la stessa cosa
319319
alert( *!*/love/i*/!*.test(str) ); // true
320320
alert( str.search(*!*/love/i*/!*) != -1 ); // true
321321
```
322322
323-
An example with the negative answer:
323+
Un esempio con la risposta negativa:
324324
325325
```js run
326326
let str = "Bla-bla-bla";
@@ -329,33 +329,33 @@ alert( *!*/love/i*/!*.test(str) ); // false
329329
alert( str.search(*!*/love/i*/!*) != -1 ); // false
330330
```
331331
332-
If the regexp has flag `pattern:g`, then `regexp.test` looks from `regexp.lastIndex` property and updates this property, just like `regexp.exec`.
332+
Se l'espressione regolare ha la flag `pattern:g`, `regexp.test` cerca dalla proprietà `regexp.lastIndex` e l'aggiorna, come fa `regexp.exec`.
333333
334-
So we can use it to search from a given position:
334+
Pertanto possiamo usarlo per cercare da una posizione data:
335335
336336
```js run
337337
let regexp = /love/gi;
338338

339339
let str = "I love JavaScript";
340340

341-
// start the search from position 10:
341+
// la ricerca inizia dalla posizione 10:
342342
regexp.lastIndex = 10;
343-
alert( regexp.test(str) ); // false (no match)
343+
alert( regexp.test(str) ); // false (nessuna corrispondenza)
344344
```
345345
346346
````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.
348348
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:
350350
351351
```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)
353353

354-
alert( regexp.test("javascript") ); // true (regexp.lastIndex=10 now)
354+
alert( regexp.test("javascript") ); // true (ora regexp.lastIndex=10)
355355
alert( regexp.test("javascript") ); // false
356356
```
357357
358-
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.
359359
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`.
361361
````

0 commit comments

Comments
 (0)