Skip to content

Commit 397391e

Browse files
authored
Multiline mode of anchors ^ $, flag "m" (#492)
* feat: translate en to uk * fix: add ` * fix: edit line 23, 67, 83
1 parent a482481 commit 397391e

File tree

1 file changed

+36
-36
lines changed
  • 9-regular-expressions/05-regexp-multiline-mode

1 file changed

+36
-36
lines changed
Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,87 @@
1-
# Multiline mode of anchors ^ $, flag "m"
1+
# Багаторядковий режим якорів ^ $, прапора "m"
22

3-
The multiline mode is enabled by the flag `pattern:m`.
3+
Багаторядковий режим вмикається прапором `pattern:m`.
44

5-
It only affects the behavior of `pattern:^` and `pattern:$`.
5+
Це впливає лише на поведінку `pattern:^` і `pattern:$`.
66

7-
In the multiline mode they match not only at the beginning and the end of the string, but also at start/end of line.
7+
У багаторядковому режимі вони збігаються не тільки на початку та в кінці тексту, а й на початку/кінці кожного рядка у тексті.
88

9-
## Searching at line start ^
9+
## Пошук на початку рядка ^
1010

11-
In the example below the text has multiple lines. The pattern `pattern:/^\d/gm` takes a digit from the beginning of each line:
11+
У прикладі нижче текст складається з кількох рядків. Шаблон `pattern:/^\d/gm` бере цифру з початку кожного рядка:
1212

1313
```js run
14-
let str = `1st place: Winnie
15-
2nd place: Piglet
16-
3rd place: Eeyore`;
14+
let str = `1 місце: Вінні-Пух
15+
2 місце: Паць
16+
3 місце: Слонопотам`;
1717

1818
*!*
1919
console.log( str.match(/^\d/gm) ); // 1, 2, 3
2020
*/!*
2121
```
2222

23-
Without the flag `pattern:m` only the first digit is matched:
23+
Без прапора `pattern:m` збігається лише перша цифра:
2424

2525
```js run
26-
let str = `1st place: Winnie
27-
2nd place: Piglet
28-
3rd place: Eeyore`;
26+
let str = `1 місце: Вінні
27+
2 місце: Паць
28+
3 місце: Слонопотам;
2929
3030
*!*
3131
console.log( str.match(/^\d/g) ); // 1
3232
*/!*
3333
```
3434

35-
That's because by default a caret `pattern:^` only matches at the beginning of the text, and in the multiline mode -- at the start of any line.
35+
Це тому, що за замовчуванням карет `pattern:^` збігається лише на початку тексту, а в багаторядковому режимі -- на початку будь-якого рядка.
3636

3737
```smart
38-
"Start of a line" formally means "immediately after a line break": the test `pattern:^` in multiline mode matches at all positions preceded by a newline character `\n`.
38+
"Початок рядка" формально означає "відразу після розриву рядка": тестовий `pattern:^` у багаторядковому режимі збігається в усіх позиціях, яким передує символ нового рядка `\n`.
3939
40-
And at the text start.
40+
І на початку тексту.
4141
```
4242

43-
## Searching at line end $
43+
## Пошук у кінці рядка $
4444

45-
The dollar sign `pattern:$` behaves similarly.
45+
Символ долара `pattern:$` поводиться аналогічно.
4646

47-
The regular expression `pattern:\d$` finds the last digit in every line
47+
Регулярний вираз `pattern:\d$` шукає останню цифру у кожному рядку
4848

4949
```js run
50-
let str = `Winnie: 1
51-
Piglet: 2
52-
Eeyore: 3`;
50+
let str = `Вінні: 1
51+
Паць: 2
52+
Слонопотам: 3`;
5353
5454
console.log( str.match(/\d$/gm) ); // 1,2,3
5555
```
5656

57-
Without the flag `pattern:m`, the dollar `pattern:$` would only match the end of the whole text, so only the very last digit would be found.
57+
Без прапора `pattern:m`, символ долара `pattern:$` відповідатиме лише кінці всього тексту, тому буде знайдено лише останню цифру.
5858

5959
```smart
60-
"End of a line" formally means "immediately before a line break": the test `pattern:$` in multiline mode matches at all positions succeeded by a newline character `\n`.
60+
"Кінець рядка" формально означає "безпосередньо перед розривом рядка": тестовий `pattern:$` у багаторядковому режимі збігається в усіх позиціях після символу нового рядка `\n`.
6161
62-
And at the text end.
62+
І в кінці тексту.
6363
```
6464

65-
## Searching for \n instead of ^ $
65+
## Шукаємо \n замість ^ $
6666

67-
To find a newline, we can use not only anchors `pattern:^` and `pattern:$`, but also the newline character `\n`.
67+
Щоб знайти новий рядок, ми можемо використовувати не лише якорі `pattern:^` і `pattern:$`, а й символ нового рядка `\n`.
6868

69-
What's the difference? Let's see an example.
69+
У чому різниця? Подивімось на приклад.
7070

71-
Here we search for `pattern:\d\n` instead of `pattern:\d$`:
71+
Тут ми шукаємо `pattern:\d\n` замість `pattern:\d$`:
7272

7373
```js run
74-
let str = `Winnie: 1
75-
Piglet: 2
76-
Eeyore: 3`;
74+
let str = `Вінні: 1
75+
Паць: 2
76+
Слонопотам: 3`;
7777
7878
console.log( str.match(/\d\n/g) ); // 1\n,2\n
7979
```
8080

81-
As we can see, there are 2 matches instead of 3.
81+
Як бачимо, 2 збіги замість 3-х.
8282

83-
That's because there's no newline after `subject:3` (there's text end though, so it matches `pattern:$`).
83+
Це тому, що після `об'єкт:3` немає нового рядка (хоча є кінець тексту, тому він відповідає `pattern:$`).
8484

85-
Another difference: now every match includes a newline character `match:\n`. Unlike the anchors `pattern:^` `pattern:$`, that only test the condition (start/end of a line), `\n` is a character, so it becomes a part of the result.
85+
Ще одна відмінність: тепер кожен збіг містить символ нового рядка `match:\n`. На відміну від якорів `pattern:^` `pattern:$`, які лише перевіряють умову (початок/кінець рядка), `\n` є символом, тому він стає частиною результату.
8686

87-
So, a `\n` in the pattern is used when we need newline characters in the result, while anchors are used to find something at the beginning/end of a line.
87+
Отже, `\n` у шаблоні використовується, коли нам потрібні символи нового рядка в результаті, тоді як якорі використовуються, щоб знайти щось на початку/кінці рядка.

0 commit comments

Comments
 (0)