Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

The regexp for an integer number is `pattern:\d+`.
Регулярний вираз для цілого числа `pattern:\d+`.

We can exclude negatives by prepending it with the negative lookbehind: `pattern:(?<!-)\d+`.
Ми можемо виключити від'ємні числа попередньо написавши регулярний вираз для негативного перегляду назад: `pattern:(?<!-)\d+`.

Although, if we try it now, we may notice one more "extra" result:
Хоча, випробувавши його, ми побачимо одне зайве співпадіння:

```js run
let regexp = /(?<!-)\d+/g;
Expand All @@ -13,11 +13,11 @@ let str = "0 12 -5 123 -18";
console.log( str.match(regexp) ); // 0, 12, 123, *!*8*/!*
```

As you can see, it matches `match:8`, from `subject:-18`. To exclude it, we need to ensure that the regexp starts matching a number not from the middle of another (non-matching) number.
Як ви бачите, шаблон знаходить `match:8`, у `subject:-18`. Щоб виключит і його, нам необхідно переконатись, що регулярний вираз починає пошук не з середини іншого не піходящого числа.

We can do it by specifying another negative lookbehind: `pattern:(?<!-)(?<!\d)\d+`. Now `pattern:(?<!\d)` ensures that a match does not start after another digit, just what we need.
Ми можемо це реалізувати вказавши додатковий вираз для негативного перегляду назад: `pattern:(?<!-)(?<!\d)\d+`. Зараз `pattern:(?<!\d)` перевіряє, щоб пошук не починався одразу після іншого числа, як нам і було потрібно.

We can also join them into a single lookbehind here:
Ми можемо об'єднати вирази в один таким чином:

```js run
let regexp = /(?<![-\d])\d+/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Find non-negative integers
# Знайдіть цілі невід'ємні числа

There's a string of integer numbers.
Дано рядок з цілих чисел.

Create a regexp that looks for only non-negative ones (zero is allowed).
Напишіть регулярний вираз, який знаходить тільки цілі невід'ємні числа (нуль допускається).

An example of use:
Приклад використання:
```js
let regexp = /your regexp/g;
let regexp = /ваш регулярний вираз/g;

let str = "0 12 -5 123 -18";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
In order to insert after the `<body>` tag, we must first find it. We can use the regular expression pattern `pattern:<body.*?>` for that.
Щоб додати інформацію після тегу `<body>` , нам потрібно спершу його знайти. Ми можемо використати для цього шаблон регулярного виразу `pattern:<body.*?>`.

In this task we don't need to modify the `<body>` tag. We only need to add the text after it.
в цьому завданні нам не потрібно змінювати тег `<body>`. Нам потрібно тільки додати текст після нього.

Here's how we can do it:
Ось таким чином ми можемо це зробити:

```js run
let str = '...<body style="...">...';
str = str.replace(/<body.*?>/, '$&<h1>Hello</h1>');
str = str.replace(/<body.*?>/, '$&<h1>Привіт</h1>');

alert(str); // ...<body style="..."><h1>Hello</h1>...
alert(str); // ...<body style="..."><h1>Привіт</h1>...
```

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>`.
в заміненому рядку `$&` означає співпадіння саме по собі, тобто, частина вихідного тексту яка відповідає шаблону `pattern:<body.*?>`. Її замінено на неї ж плюс `<h1>Привіт</h1>`.

An alternative is to use lookbehind:
Альнернативою було би використання перегляду назад:

```js run
let str = '...<body style="...">...';
str = str.replace(/(?<=<body.*?>)/, `<h1>Hello</h1>`);
str = str.replace(/(?<=<body.*?>)/, `<h1>Привіт</h1>`);

alert(str); // ...<body style="..."><h1>Hello</h1>...
alert(str); // ...<body style="..."><h1>привіт</h1>...
```

As you can see, there's only lookbehind part in this regexp.
Як бачите, є тільки перегляд назад у цьому регулярному виразі.

It works like this:
- At every position in the text.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Insert After Head
# Додайте після тегу Head

We have a string with an HTML Document.
У нас є рядок з HTML-документом.

Write a regular expression that inserts `<h1>Hello</h1>` immediately after `<body>` tag. The tag may have attributes.
Напишіть регулярний вираз який вставляє `<h1>Привіт</h1>` одразу після тегу `<body>`. Тег може мати атрибути.

For instance:
Приклад:

```js
let regexp = /your regular expression/;
let regexp = /ваш регулярний вираз/;

let str = `
<html>
Expand All @@ -17,13 +17,13 @@ let str = `
</html>
`;

str = str.replace(regexp, `<h1>Hello</h1>`);
str = str.replace(regexp, `<h1>Привіт</h1>`);
```

After that the value of `str` should be:
Після цього значення `str` має бути:
```html
<html>
<body style="height: 200px"><h1>Hello</h1>
<body style="height: 200px"><h1>Привіт</h1>
...
</body>
</html>
Expand Down
Loading