|
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 | +Щоб додати інформацію після тегу `<body>` , нам потрібно спершу його знайти. Ми можемо використати для цього шаблон регулярного виразу `pattern:<body.*?>`. |
2 | 2 |
|
3 | | -In this task we don't need to modify the `<body>` tag. We only need to add the text after it. |
| 3 | +в цьому завданні нам не потрібно змінювати тег `<body>`. Нам потрібно тільки додати текст після нього. |
4 | 4 |
|
5 | | -Here's how we can do it: |
| 5 | +Ось таким чином ми можемо це зробити: |
6 | 6 |
|
7 | 7 | ```js run |
8 | 8 | let str = '...<body style="...">...'; |
9 | | -str = str.replace(/<body.*?>/, '$&<h1>Hello</h1>'); |
| 9 | +str = str.replace(/<body.*?>/, '$&<h1>Привіт</h1>'); |
10 | 10 |
|
11 | | -alert(str); // ...<body style="..."><h1>Hello</h1>... |
| 11 | +alert(str); // ...<body style="..."><h1>Привіт</h1>... |
12 | 12 | ``` |
13 | 13 |
|
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 | +в заміненому рядку `$&` означає співпадіння саме по собі, тобто, частина вихідного тексту яка відповідає шаблону `pattern:<body.*?>`. Її замінено на неї ж плюс `<h1>Привіт</h1>`. |
15 | 15 |
|
16 | | -An alternative is to use lookbehind: |
| 16 | +Альнернативою було би використання перегляду назад: |
17 | 17 |
|
18 | 18 | ```js run |
19 | 19 | let str = '...<body style="...">...'; |
20 | | -str = str.replace(/(?<=<body.*?>)/, `<h1>Hello</h1>`); |
| 20 | +str = str.replace(/(?<=<body.*?>)/, `<h1>Привіт</h1>`); |
21 | 21 |
|
22 | | -alert(str); // ...<body style="..."><h1>Hello</h1>... |
| 22 | +alert(str); // ...<body style="..."><h1>привіт</h1>... |
23 | 23 | ``` |
24 | 24 |
|
25 | | -As you can see, there's only lookbehind part in this regexp. |
| 25 | +Як бачите, є тільки перегляд назад у цьому регулярному виразі. |
26 | 26 |
|
27 | 27 | It works like this: |
28 | 28 | - At every position in the text. |
|
0 commit comments