Merge pull request #1707 from imabp/master

Translated tasks to English Language.
This commit is contained in:
Ilya Kantor 2020-01-19 19:59:18 +01:00 committed by GitHub
commit b5ea34c83a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 15 deletions

View file

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

View file

@ -1,13 +1,13 @@
# Вставьте после фрагмента # Insert After Head
Есть строка с HTML-документом. There is a line with an HTML Document.
Вставьте после тега `<body>` (у него могут быть атрибуты) строку `<h1>Hello</h1>`. Insert after tag `<body>` (it may have attributes) line `<h1>Hello</h1>`.
Например: For instance:
```js ```js
let regexp = /ваше регулярное выражение/; let regexp = /your regular expression/;
let str = ` let str = `
<html> <html>
@ -20,7 +20,7 @@ let str = `
str = str.replace(regexp, `<h1>Hello</h1>`); str = str.replace(regexp, `<h1>Hello</h1>`);
``` ```
После этого значение `str`: After that value `str`:
```html ```html
<html> <html>
<body style="height: 200px"><h1>Hello</h1> <body style="height: 200px"><h1>Hello</h1>