Translated Solution from Russian to English
This commit is contained in:
parent
2bc3c29ef2
commit
8523ab7c23
1 changed files with 8 additions and 9 deletions
|
@ -1,9 +1,8 @@
|
||||||
|
In order to insert after the `<body>` tag, you must first find it. We will use the regular expression pattern `pattern:<body.*>`.
|
||||||
|
|
||||||
Для того, чтобы вставить после тега `<body>`, нужно вначале его найти. Будем использовать регулярное выражение `pattern:<body.*>`.
|
Next, we need to leave the `<body>` tag in place and add text after it.
|
||||||
|
|
||||||
Далее, нам нужно оставить сам тег `<body>` на месте и добавить текст после него.
|
This can be done like this:
|
||||||
|
|
||||||
Это можно сделать вот так:
|
|
||||||
```js run
|
```js run
|
||||||
let str = '...<body style="...">...';
|
let str = '...<body style="...">...';
|
||||||
str = str.replace(/<body.*>/, '$&<h1>Hello</h1>');
|
str = str.replace(/<body.*>/, '$&<h1>Hello</h1>');
|
||||||
|
@ -11,9 +10,9 @@ str = str.replace(/<body.*>/, '$&<h1>Hello</h1>');
|
||||||
alert(str); // ...<body style="..."><h1>Hello</h1>...
|
alert(str); // ...<body style="..."><h1>Hello</h1>...
|
||||||
```
|
```
|
||||||
|
|
||||||
В строке замены `$&` означает само совпадение, то есть мы заменяем `pattern:<body.*>` заменяется на самого себя плюс `<h1>Hello</h1>`.
|
In the replacement string `$&` means the match itself, that is, we replace `pattern:<body.*>` Is replaced by itself plus `<h1>Hello</h1>`.
|
||||||
|
|
||||||
Альтернативный вариант - использовать ретроспективную проверку:
|
An alternative is to use retrospective validation:
|
||||||
|
|
||||||
```js run
|
```js run
|
||||||
let str = '...<body style="...">...';
|
let str = '...<body style="...">...';
|
||||||
|
@ -22,8 +21,8 @@ str = str.replace(/(?<=<body.*>)/, `<h1>Hello</h1>`);
|
||||||
alert(str); // ...<body style="..."><h1>Hello</h1>...
|
alert(str); // ...<body style="..."><h1>Hello</h1>...
|
||||||
```
|
```
|
||||||
|
|
||||||
Такое регулярное выражение на каждой позиции будет проверять, не идёт ли прямо перед ней `pattern:<body.*>`. Если да - совпадение найдено. Но сам тег `pattern:<body.*>` в совпадение не входит, он только участвует в проверке. А других символов после проверки в нём нет, так что текст совпадения будет пустым.
|
Such a regular expression at each position will check if `pattern:<body.*>`does not go directly in front of it. If yes, a match is found. But the tag `pattern:<body.*>` does not coincide, it only participates in the verification. And there are no other characters after checking in it, so the match text will be empty.
|
||||||
|
|
||||||
Происходит замена "пустой строки", перед которой идёт `pattern:<body.*>` на `<h1>Hello</h1>`. Что, как раз, и есть вставка этой строки после `<body>`.
|
This replaces the "empty line", followed by `pattern:<body.*>` With `<h1>Hello</h1>`. Which, exactly, is the insertion of this line after `<body>`.
|
||||||
|
|
||||||
P.S. Этому регулярному выражению не помешают флаги: `pattern:/<body.*>/si`, чтобы в "точку" входил перевод строки (тег может занимать несколько строк), а также чтобы теги в другом регистре типа `match:<BODY>` тоже находились.
|
P.S. The flags: `pattern:/<body.*>/si`, will not interfere with this regular expression, so that a line break appears in the "dot" (a tag can span several lines), and also that the tags are in a different register of the `match:<BODY>` type, too.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue