Skip to content

Commit 528c5df

Browse files
committed
Update 9-regular-expressions\14-regexp-lookahead-lookbehind
1 parent 710504b commit 528c5df

File tree

1 file changed

+13
-13
lines changed
  • 9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head

1 file changed

+13
-13
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
In order to insert after the `<body>` tag, we must first find it. We can use the regular expression pattern `pattern:<body.*?>` for that.
1+
Per inserire qualcosa dopo il tag `<body>` dobbiamo prima trovarlo. A questo scopo possiamo usare l'espressione regolare `pattern:<body.*?>`.
22

3-
In this task we don't need to modify the `<body>` tag. We only need to add the text after it.
3+
In questa esercitazione non abbiamo bisogno di modificare il tag `<body>`. Dobbiamo solo aggiungere del testo dopo di esso.
44

5-
Here's how we can do it:
5+
Ecco come possiamo farlo:
66

77
```js run
88
let str = '...<body style="...">...';
@@ -11,9 +11,9 @@ str = str.replace(/<body.*?>/, '$&<h1>Hello</h1>');
1111
alert(str); // ...<body style="..."><h1>Hello</h1>...
1212
```
1313

14-
In the replacement string `$&` means the match itself, that is, the part of the source text that corresponds to `pattern:<body.*?>`. It gets replaced by itself plus `<h1>Hello</h1>`.
14+
Nella stringa di sostituzione `$&` identifica la stessa corrispondenza, in altre parole, la parte della stringa sorgente che trova riscontro con `pattern:<body.*?>`. Essa viene sostituita da se stessa più l'aggiunta di `<h1>Hello</h1>`.
1515

16-
An alternative is to use lookbehind:
16+
L'uso del lookbehind costituisce un'alternativa:
1717

1818
```js run
1919
let str = '...<body style="...">...';
@@ -22,15 +22,15 @@ str = str.replace(/(?<=<body.*?>)/, `<h1>Hello</h1>`);
2222
alert(str); // ...<body style="..."><h1>Hello</h1>...
2323
```
2424

25-
As you can see, there's only lookbehind part in this regexp.
25+
Come potete osservare, c'è solo la parte di lookbehind in questa regexp.
2626

27-
It works like this:
28-
- At every position in the text.
29-
- Check if it's preceeded by `pattern:<body.*?>`.
30-
- If it's so then we have the match.
27+
Funziona in questo modo:
28+
- Per ogni posizione nella stringa.
29+
- Verifica se è preceduta da `pattern:<body.*?>`.
30+
- In caso affermativo abbiamo trovato la corrispondenza.
3131

32-
The tag `pattern:<body.*?>` won't be returned. The result of this regexp is literally an empty string, but it matches only at positions preceeded by `pattern:<body.*?>`.
32+
Il tag `pattern:<body.*?>` non verrà restituito. Il risultato di questa regexp è letteralmente una stringa vuota, ma individua le posizioni precedute da `pattern:<body.*?>`.
3333

34-
So it replaces the "empty line", preceeded by `pattern:<body.*?>`, with `<h1>Hello</h1>`. That's the insertion after `<body>`.
34+
Quindi sostituisce uno "spazio vuoto" preceduto da `pattern:<body.*?>`, con `<h1>Hello</h1>`. In altre parole effettua un inserimento dopo `<body>`.
3535

36-
P.S. Regexp flags, such as `pattern:s` and `pattern:i` can also be useful: `pattern:/<body.*?>/si`. The `pattern:s` flag makes the dot `pattern:.` match a newline character, and `pattern:i` flag makes `pattern:<body>` also match `match:<BODY>` case-insensitively.
36+
P.S. I flag `pattern:s` e `pattern:i` potrebbero inoltre risultare utili: `pattern:/<body.*?>/si`. Il flag `pattern:s` fa in modo che il `pattern:.` identifichi anche un carattere di nuova riga, e con il flag `pattern:i` otteniamo che `pattern:<body>` e `match:<BODY>` costituiscano entrambi un riscontro.

0 commit comments

Comments
 (0)