From 9f58af411a0a05485324806b031b6f196feb3dcc Mon Sep 17 00:00:00 2001 From: vitaliimalynka Date: Mon, 12 Dec 2022 22:39:10 +0200 Subject: [PATCH 1/4] [backreferences] translated article about backreferences --- .../12-regexp-backreferences/article.md | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/9-regular-expressions/12-regexp-backreferences/article.md b/9-regular-expressions/12-regexp-backreferences/article.md index b80fa85cf..e7f9f5fd7 100644 --- a/9-regular-expressions/12-regexp-backreferences/article.md +++ b/9-regular-expressions/12-regexp-backreferences/article.md @@ -1,21 +1,21 @@ -# Backreferences in pattern: \N and \k +# Зворотні посилання в шаблоні: \N і \k<ім’я> -We can use the contents of capturing groups `pattern:(...)` not only in the result or in the replacement string, but also in the pattern itself. +Вміст груп захоплення `pattern:(...)` можна використовувати не тільки в резульаті пошуку чи рядку заміни, а й також безпосередньо у самому шаблоні. -## Backreference by number: \N +## Зворотнє посилання за номером: \N -A group can be referenced in the pattern using `pattern:\N`, where `N` is the group number. +До групи можна посилатися в шаблоні використовуючи `pattern:\N`, де `N` - номер групи. -To make clear why that's helpful, let's consider a task. +Для того, аби зрозуміти, чим це може бути корисним, давайте розглянемо приклад. -We need to find quoted strings: either single-quoted `subject:'...'` or a double-quoted `subject:"..."` -- both variants should match. +Потрібно знайти рядки в лапках: або в одинарних `subject:'...'`, або в подвійних `subject:"..."` -- обидва варіанти повинні збігатися. -How to find them? +Як їх знайти? -We can put both kinds of quotes in the square brackets: `pattern:['"](.*?)['"]`, but it would find strings with mixed quotes, like `match:"...'` and `match:'..."`. That would lead to incorrect matches when one quote appears inside other ones, like in the string `subject:"She's the one!"`: +Ми можемо розмістити обидва типи лапок у квадратних дужках: `pattern:['"](.*?)['"]`, але цей вираз знайде рядки зі змішаними лапками, наприклад `match:"...'` і `match:'..."`. Це призведе до неправильних збігів, коли один тип лапок буде в середині інших, наприклад в рядку `subject:"She's the one!"`: ```js run -let str = `He said: "She's the one!".`; +let str = `Він сказав: "She's the one!".`; let regexp = /['"](.*?)['"]/g; @@ -23,14 +23,14 @@ let regexp = /['"](.*?)['"]/g; alert( str.match(regexp) ); // "She' ``` -As we can see, the pattern found an opening quote `match:"`, then the text is consumed till the other quote `match:'`, that closes the match. +Як видно, шаблон знайшов початкову подвійну лапку `match:"`, потім текст, включно до наступної одинарної лапки `match:'`, опісля чого пошук завершився. -To make sure that the pattern looks for the closing quote exactly the same as the opening one, we can wrap it into a capturing group and backreference it: `pattern:(['"])(.*?)\1`. +Щоб бути впевненим, що шаблон шукає таку ж кінцеву лапку, як і початкову, можемо обернути початкові лапки в групу захвату і задіяти зворотнє посилання: `pattern:(['"])(.*?)\1`. -Here's the correct code: +Ось правильний код: ```js run -let str = `He said: "She's the one!".`; +let str = `Він сказав: "She's the one!".`; *!* let regexp = /(['"])(.*?)\1/g; @@ -39,30 +39,30 @@ let regexp = /(['"])(.*?)\1/g; alert( str.match(regexp) ); // "She's the one!" ``` -Now it works! The regular expression engine finds the first quote `pattern:(['"])` and memorizes its content. That's the first capturing group. +Все працює! Механізм регулярних виразів знаходить першу лапку `pattern:(['"])` та запамʼятовує її. Це перша група захоплення. -Further in the pattern `pattern:\1` means "find the same text as in the first group", exactly the same quote in our case. +Далі в шаблоні `pattern:\1` означає: "знайти такий же текст, як і в першій групі", що в нашому випадку - таку ж саму лапку. -Similar to that, `pattern:\2` would mean the contents of the second group, `pattern:\3` - the 3rd group, and so on. +Подібно до цього, `pattern:\2` означало б вміст другої групи, `pattern:\3` - 3-ї групи, і так далі. ```smart -If we use `?:` in the group, then we can't reference it. Groups that are excluded from capturing `(?:...)` are not memorized by the engine. +Ми не можемо посилатися на групу, якщо в ній задіяно `?:`. Групи, які були виключені із захоплення `(?:...)`, не запамʼятовуються механізмом регулярних виразів. ``` -```warn header="Don't mess up: in the pattern `pattern:\1`, in the replacement: `pattern:$1`" -In the replacement string we use a dollar sign: `pattern:$1`, while in the pattern - a backslash `pattern:\1`. +```warn header="Не переплутайте: в шаблоні `pattern:\1`, при заміні: `pattern:$1`" +У рядку заміни використовується знак долара: `pattern:$1`, а в шаблоні - обернена коса риска `pattern:\1`. ``` -## Backreference by name: `\k` +## Зворотнє посилання за назвою: `\k<імʼя>` -If a regexp has many parentheses, it's convenient to give them names. +Якщо регулярний вираз має багато дужок, досить зручно в такому випадку давати їм імена. -To reference a named group we can use `pattern:\k`. +Для посилання на іменовану групу можна використовувати `pattern:\k<імʼя>`. -In the example below the group with quotes is named `pattern:?`, so the backreference is `pattern:\k`: +У наведенемо нижче прикладі група з лапками називається `pattern:?`, тому звернення до нього буде `pattern:\k`: ```js run -let str = `He said: "She's the one!".`; +let str = `Він сказав: "She's the one!".`; *!* let regexp = /(?['"])(.*?)\k/g; From 24a74f5aad7210cc5c90b0ad141842f06176fc2d Mon Sep 17 00:00:00 2001 From: Taras Date: Wed, 24 May 2023 22:03:25 +0300 Subject: [PATCH 2/4] Update 9-regular-expressions/12-regexp-backreferences/article.md --- 9-regular-expressions/12-regexp-backreferences/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/12-regexp-backreferences/article.md b/9-regular-expressions/12-regexp-backreferences/article.md index e7f9f5fd7..24b7101ea 100644 --- a/9-regular-expressions/12-regexp-backreferences/article.md +++ b/9-regular-expressions/12-regexp-backreferences/article.md @@ -12,7 +12,7 @@ Як їх знайти? -Ми можемо розмістити обидва типи лапок у квадратних дужках: `pattern:['"](.*?)['"]`, але цей вираз знайде рядки зі змішаними лапками, наприклад `match:"...'` і `match:'..."`. Це призведе до неправильних збігів, коли один тип лапок буде в середині інших, наприклад в рядку `subject:"She's the one!"`: +Ми можемо розмістити обидва типи лапок у квадратних дужках: `pattern:['"](.*?)['"]`, але цей вираз знайде рядки зі змішаними лапками, наприклад `match:"...'` і `match:'..."`. Це призведе до неправильних збігів, коли один тип лапок буде в середині інших, наприклад в рядку `subject:"Яке твоє ім'я?"`: ```js run let str = `Він сказав: "She's the one!".`; From 8817ec8a675d340d54d65fdf790faf02b9405b65 Mon Sep 17 00:00:00 2001 From: Taras Date: Wed, 24 May 2023 22:09:45 +0300 Subject: [PATCH 3/4] Apply suggestions from code review --- .../12-regexp-backreferences/article.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/9-regular-expressions/12-regexp-backreferences/article.md b/9-regular-expressions/12-regexp-backreferences/article.md index 24b7101ea..0453a53dd 100644 --- a/9-regular-expressions/12-regexp-backreferences/article.md +++ b/9-regular-expressions/12-regexp-backreferences/article.md @@ -15,12 +15,12 @@ Ми можемо розмістити обидва типи лапок у квадратних дужках: `pattern:['"](.*?)['"]`, але цей вираз знайде рядки зі змішаними лапками, наприклад `match:"...'` і `match:'..."`. Це призведе до неправильних збігів, коли один тип лапок буде в середині інших, наприклад в рядку `subject:"Яке твоє ім'я?"`: ```js run -let str = `Він сказав: "She's the one!".`; +let str = `Він запитав: "Яке твоє ім'я?".`; let regexp = /['"](.*?)['"]/g; -// The result is not what we'd like to have -alert( str.match(regexp) ); // "She' +// Результат не той, який ми б хотіли мати +alert( str.match(regexp) ); // "Яке твоє ім' ``` Як видно, шаблон знайшов початкову подвійну лапку `match:"`, потім текст, включно до наступної одинарної лапки `match:'`, опісля чого пошук завершився. @@ -30,13 +30,13 @@ alert( str.match(regexp) ); // "She' Ось правильний код: ```js run -let str = `Він сказав: "She's the one!".`; +let str = `Він запитав: "Яке твоє ім'я?".`; *!* let regexp = /(['"])(.*?)\1/g; */!* -alert( str.match(regexp) ); // "She's the one!" +alert( str.match(regexp) ); // "Яке твоє ім'я?" ``` Все працює! Механізм регулярних виразів знаходить першу лапку `pattern:(['"])` та запамʼятовує її. Це перша група захоплення. @@ -62,7 +62,7 @@ alert( str.match(regexp) ); // "She's the one!" У наведенемо нижче прикладі група з лапками називається `pattern:?`, тому звернення до нього буде `pattern:\k`: ```js run -let str = `Він сказав: "She's the one!".`; +let str = `Він запитав: "Яке твоє ім'я?".`; *!* let regexp = /(?['"])(.*?)\k/g; From 23578ab15c6df34e4cb0c2206a30f76e9e3cdc24 Mon Sep 17 00:00:00 2001 From: Taras Date: Wed, 24 May 2023 22:11:53 +0300 Subject: [PATCH 4/4] Update article.md --- 9-regular-expressions/12-regexp-backreferences/article.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/9-regular-expressions/12-regexp-backreferences/article.md b/9-regular-expressions/12-regexp-backreferences/article.md index 0453a53dd..15d20960b 100644 --- a/9-regular-expressions/12-regexp-backreferences/article.md +++ b/9-regular-expressions/12-regexp-backreferences/article.md @@ -59,7 +59,7 @@ alert( str.match(regexp) ); // "Яке твоє ім'я?" Для посилання на іменовану групу можна використовувати `pattern:\k<імʼя>`. -У наведенемо нижче прикладі група з лапками називається `pattern:?`, тому звернення до нього буде `pattern:\k`: +У наведеному нижче прикладі група з лапками називається `pattern:?`, тому звернення до нього буде `pattern:\k`: ```js run let str = `Він запитав: "Яке твоє ім'я?".`; @@ -68,5 +68,5 @@ let str = `Він запитав: "Яке твоє ім'я?".`; let regexp = /(?['"])(.*?)\k/g; */!* -alert( str.match(regexp) ); // "She's the one!" +alert( str.match(regexp) ); // "Яке твоє ім'я?" ```