diff --git a/9-regular-expressions/09-regexp-groups/1-find-webcolor-3-or-6/solution.md b/9-regular-expressions/09-regexp-groups/1-find-webcolor-3-or-6/solution.md index d653ff970a..7af0f697ac 100644 --- a/9-regular-expressions/09-regexp-groups/1-find-webcolor-3-or-6/solution.md +++ b/9-regular-expressions/09-regexp-groups/1-find-webcolor-3-or-6/solution.md @@ -1,14 +1,14 @@ -A regexp to search 3-digit color `#abc`: `pattern:/#[a-f0-9]{3}/i`. +Регулярное выражение для поиска номера цвета из трёх символов `#abc`: `pattern:/#[a-f0-9]{3}/i`. -We can add exactly 3 more optional hex digits. We don't need more or less. Either we have them or we don't. +Мы можем задать ещё ровно 3 дополнительных шестнадцатеричных цифры. Нам не нужно больше или меньше - в цвете либо 3, либо 6 цифр. -The simplest way to add them -- is to append to the regexp: `pattern:/#[a-f0-9]{3}([a-f0-9]{3})?/i` +Простейший способ добавить их -- добавить в регулярное выражение: `pattern:/#[a-f0-9]{3}([a-f0-9]{3})?/i` -We can do it in a smarter way though: `pattern:/#([a-f0-9]{3}){1,2}/i`. +Мы можем сделать это более интересным способом: `pattern:/#([a-f0-9]{3}){1,2}/i`. -Here the regexp `pattern:[a-f0-9]{3}` is in parentheses to apply the quantifier `pattern:{1,2}` to it as a whole. +Регулярное выражение `pattern:[a-f0-9]{3}` заключено в скобки для корректного применения к нему квантификатора `pattern:{1,2}`. -In action: +В действии: ```js run let reg = /#([a-f0-9]{3}){1,2}/gi; @@ -18,7 +18,7 @@ let str = "color: #3f3; background-color: #AA00ef; and: #abcd"; alert( str.match(reg) ); // #3f3 #AA00ef #abc ``` -There's a minor problem here: the pattern found `match:#abc` in `subject:#abcd`. To prevent that we can add `pattern:\b` to the end: +Здесь есть небольшая проблема: шаблон находит `match:#abc` в `subject:#abcd`. Чтобы предотвратить это, мы можем добавить `pattern:\b` в конец: ```js run let reg = /#([a-f0-9]{3}){1,2}\b/gi; diff --git a/9-regular-expressions/09-regexp-groups/1-find-webcolor-3-or-6/task.md b/9-regular-expressions/09-regexp-groups/1-find-webcolor-3-or-6/task.md index 4efd6f61f8..f0dad1b0c3 100644 --- a/9-regular-expressions/09-regexp-groups/1-find-webcolor-3-or-6/task.md +++ b/9-regular-expressions/09-regexp-groups/1-find-webcolor-3-or-6/task.md @@ -1,14 +1,14 @@ -# Find color in the format #abc or #abcdef +# Найти цвет в формате #abc или #abcdef -Write a RegExp that matches colors in the format `#abc` or `#abcdef`. That is: `#` followed by 3 or 6 hexadecimal digits. +Напишите регулярное выражение, которое соответствует цветам в формате `#abc` или `#abcdef`. То есть: `#` и за ним 3 или 6 шестнадцатеричных цифр. -Usage example: +Пример использования: ```js -let reg = /your regexp/g; +let reg = /ваш регэксп/g; let str = "color: #3f3; background-color: #AA00ef; and: #abcd"; alert( str.match(reg) ); // #3f3 #AA00ef ``` -P.S. This should be exactly 3 or 6 hex digits: values like `#abcd` should not match. +P.S. Это должно быть ровно 3 или 6 шестнадцатеричных цифр. При этом значения типа `#abcd` не должны совпадать в результат. diff --git a/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/solution.md b/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/solution.md index 23065413ef..4898f79ee4 100644 --- a/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/solution.md +++ b/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/solution.md @@ -1,13 +1,13 @@ -An non-negative integer number is `pattern:\d+`. We should exclude `0` as the first digit, as we don't need zero, but we can allow it in further digits. +Регулярное выражение для неотрицательного целого числа `pattern:\d+`. Мы должны исключить `0` в качестве первой цифры, так как нам не нужен ноль, но мы можем разрешить его появление далее. -So that gives us `pattern:[1-9]\d*`. +Нам позволит сделать это регулярное выражение: `pattern:[1-9]\d*`. -A decimal part is: `pattern:\.\d+`. +Десятичная часть находится с помощью: `pattern:\.\d+`. -Because the decimal part is optional, let's put it in parentheses with the quantifier `pattern:'?'`. +Поскольку десятичная часть является необязательной, то давайте заключим ее в скобки с квантификатором `pattern:'?'`. -Finally we have the regexp: `pattern:[1-9]\d*(\.\d+)?`: +В итоге, мы получаем регулярное выражение: `pattern:[1-9]\d*(\.\d+)?`: ```js run let reg = /[1-9]\d*(\.\d+)?/g; diff --git a/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/task.md b/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/task.md index ad8c81eaec..1191ffe35d 100644 --- a/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/task.md +++ b/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/task.md @@ -1,12 +1,12 @@ -# Find positive numbers +# Найти положительные числа -Create a regexp that looks for positive numbers, including those without a decimal point. +Создайте регулярное выражение, которое ищет положительные числа, включая числа без десятичной части. -An example of use: +Пример использования: ```js -let reg = /your regexp/g; +let reg = /ваш регэксп/g; let str = "1.5 0 -5 12. 123.4."; -alert( str.match(reg) ); // 1.5, 12, 123.4 (ignores 0 and -5) +alert( str.match(reg) ); // 1.5, 12, 123.4 (игнорирует 0 и -5) ``` diff --git a/9-regular-expressions/09-regexp-groups/4-find-decimal-numbers/solution.md b/9-regular-expressions/09-regexp-groups/4-find-decimal-numbers/solution.md index dd2410847b..84826d8a9b 100644 --- a/9-regular-expressions/09-regexp-groups/4-find-decimal-numbers/solution.md +++ b/9-regular-expressions/09-regexp-groups/4-find-decimal-numbers/solution.md @@ -1,6 +1,6 @@ -A positive number with an optional decimal part is (per previous task): `pattern:\d+(\.\d+)?`. +Положительное число с необязательным присутствием десятичной части (из прошлой задачи): `pattern:\d+(\.\d+)?`. -Let's add an optional `-` in the beginning: +Давайте добавим необязательный `-` в начало: ```js run let reg = /-?\d+(\.\d+)?/g; diff --git a/9-regular-expressions/09-regexp-groups/4-find-decimal-numbers/task.md b/9-regular-expressions/09-regexp-groups/4-find-decimal-numbers/task.md index 121a18a41b..be6245689d 100644 --- a/9-regular-expressions/09-regexp-groups/4-find-decimal-numbers/task.md +++ b/9-regular-expressions/09-regexp-groups/4-find-decimal-numbers/task.md @@ -1,11 +1,11 @@ -# Find all numbers +# Найти все числа -Write a regexp that looks for all decimal numbers including integer ones, with the floating point and negative ones. +Напишите регулярное выражение, которое ищет любые десятичные числа, включая целочисленные, с плавающей точкой и отрицательные. -An example of use: +Пример использования: ```js -let reg = /your regexp/g; +let reg = /ваше выражение/g; let str = "-1.5 0 2 -123.4."; diff --git a/9-regular-expressions/09-regexp-groups/5-parse-expression/solution.md b/9-regular-expressions/09-regexp-groups/5-parse-expression/solution.md index 3db5f667c2..bdb6d2bcd7 100644 --- a/9-regular-expressions/09-regexp-groups/5-parse-expression/solution.md +++ b/9-regular-expressions/09-regexp-groups/5-parse-expression/solution.md @@ -1,16 +1,16 @@ -A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in previous tasks. +Регулярное выражение для числа: `pattern:-?\d+(\.\d+)?`. Мы создали его в предыдущих задачах. -An operator is `pattern:[-+*/]`. We put the dash `pattern:-` first, because in the middle it would mean a character range, we don't need that. +Регулярное выражение для оператора `pattern:[-+*/]`. Мы вставили тире `pattern:-` в начало выражения, потому что в середине этот символ будет означать диапазон, а нам это не нужно. -Note that a slash should be escaped inside a JavaScript regexp `pattern:/.../`. +Отметим, что косая черта должна быть экранирована внутри регулярного выражения JavaScript `pattern:/.../`. -We need a number, an operator, and then another number. And optional spaces between them. +Нам необходимо число, оператор и, затем, другие числа. И необязательные символы пробела между ними. -The full regular expression: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`. +Полное выражение: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`. -To get a result as an array let's put parentheses around the data that we need: numbers and the operator: `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`. +Для получения результата в виде массива давайте вставим скобки вокруг данных, которые нам необходимы: чисел и операторов: `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`. -In action: +В действии: ```js run let reg = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/; @@ -18,22 +18,22 @@ let reg = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/; alert( "1.2 + 12".match(reg) ); ``` -The result includes: +Результат `result` включает в себя: -- `result[0] == "1.2 + 12"` (full match) -- `result[1] == "1.2"` (first group `(-?\d+(\.\d+)?)` -- the first number, including the decimal part) -- `result[2] == ".2"` (second group`(\.\d+)?` -- the first decimal part) -- `result[3] == "+"` (third group `([-+*\/])` -- the operator) -- `result[4] == "12"` (forth group `(-?\d+(\.\d+)?)` -- the second number) -- `result[5] == undefined` (fifth group `(\.\d+)?` -- the last decimal part is absent, so it's undefined) +- `result[0] == "1.2 + 12"` (полное совпадение) +- `result[1] == "1.2"` (первая группа `(-?\d+(\.\d+)?)` -- первое число, включая десятичную часть) +- `result[2] == ".2"` (вторая группа `(\.\d+)?` -- первая десятичная часть) +- `result[3] == "+"` (третья группа `([-+*\/])` -- оператор) +- `result[4] == "12"` (чертвертая группа `(-?\d+(\.\d+)?)` -- второе число) +- `result[5] == undefined` (пятая группа `(\.\d+)?` -- вторая десятичная часть отсутствует, поэтому значение `undefined`) -We only want the numbers and the operator, without the full match or the decimal parts. +Нам необходимы только числа и оператор без полного совпадения или десятичной части. -The full match (the arrays first item) can be removed by shifting the array `pattern:result.shift()`. +Полное совпадение (первый элемент массива) может быть удален при помощи сдвига массива `pattern:result.shift()`. -The decimal groups can be removed by making them into non-capturing groups, by adding `pattern:?:` to the beginning: `pattern:(?:\.\d+)?`. +От десятичных групп можно избавиться, если исключить захват скобочной группы, добавив `pattern:?:` в начало: `pattern:(?:\.\d+)?`. -The final solution: +Итоговое решение: ```js run function parse(expr) { diff --git a/9-regular-expressions/09-regexp-groups/5-parse-expression/task.md b/9-regular-expressions/09-regexp-groups/5-parse-expression/task.md index 8b54d46832..c40452279e 100644 --- a/9-regular-expressions/09-regexp-groups/5-parse-expression/task.md +++ b/9-regular-expressions/09-regexp-groups/5-parse-expression/task.md @@ -1,23 +1,23 @@ -# Parse an expression +# Разобрать выражение -An arithmetical expression consists of 2 numbers and an operator between them, for instance: +Арифметическое выражение включает два числа и оператор между ними. Например: - `1 + 2` - `1.2 * 3.4` - `-3 / -6` - `-2 - 2` -The operator is one of: `"+"`, `"-"`, `"*"` or `"/"`. +Оператором может быть: `"+"`, `"-"`, `"*"` или `"/"`. -There may be extra spaces at the beginning, at the end or between the parts. +В выражении могут быть пробелы в начале, в конце или между частями выражения. -Create a function `parse(expr)` that takes an expression and returns an array of 3 items: +Создайте функцию `parse(expr)`, которая принимает выражение и возвращает массив из трёх элементов: -1. The first number. -2. The operator. -3. The second number. +1. Первое число. +2. Оператор. +3. Второе число. -For example: +Например: ```js let [a, op, b] = parse("1.2 * 3.4"); diff --git a/9-regular-expressions/09-regexp-groups/article.md b/9-regular-expressions/09-regexp-groups/article.md index 6b2a17f11b..051653e3eb 100644 --- a/9-regular-expressions/09-regexp-groups/article.md +++ b/9-regular-expressions/09-regexp-groups/article.md @@ -1,41 +1,41 @@ -# Capturing groups +# Скобочные группы -A part of a pattern can be enclosed in parentheses `pattern:(...)`. This is called a "capturing group". +Часть шаблона можно заключить в скобки `pattern:(...)`. Это называется "скобочная группа". -That has two effects: +У такого выделения есть два эффекта: -1. It allows to place a part of the match into a separate array. -2. If we put a quantifier after the parentheses, it applies to the parentheses as a whole, not the last character. +1. Позволяет поместить часть совпадения в отдельный массив. +2. Если установить квантификтор после скобок, то он будет применяться ко всему содержимому скобки, а не к одному символу. -## Example +## Пример -In the example below the pattern `pattern:(go)+` finds one or more `match:'go'`: +В примере ниже шаблон `pattern:(go)+` ищет как минимум одно совпадение с `match:'go'`: ```js run alert( 'Gogogo now!'.match(/(go)+/i) ); // "Gogogo" ``` -Without parentheses, the pattern `pattern:/go+/` means `subject:g`, followed by `subject:o` repeated one or more times. For instance, `match:goooo` or `match:gooooooooo`. +Без скобок, шаблон `pattern:/go+/` означает символ `subject:g` и идущий после него символ `subject:o`, который повторяется один или более раз. Например, `match:goooo` или `match:gooooooooo`. -Parentheses group the word `pattern:(go)` together. +Скобки группируют символы в слово `pattern:(go)`. -Let's make something more complex -- a regexp to match an email. +Сделаем что-то более сложное -- регулярное выражение, которое соответствует адресу электронной почты. -Examples of emails: +Пример такой почты: ``` my@mail.com john.smith@site.com.uk ``` -The pattern: `pattern:[-.\w]+@([\w-]+\.)+[\w-]{2,20}`. +Шаблон: `pattern:[-.\w]+@([\w-]+\.)+[\w-]{2,20}`. -1. The first part `pattern:[-.\w]+` (before `@`) may include any alphanumeric word characters, a dot and a dash, to match `match:john.smith`. -2. Then `pattern:@`, and the domain. It may be a subdomain like `host.site.com.uk`, so we match it as "a word followed by a dot `pattern:([\w-]+\.)` (repeated), and then the last part must be a word: `match:com` or `match:uk` (but not very long: 2-20 characters). +1. Первая часть `pattern:[-.\w]+` (перед `@`) может включать любые числовые или буквенные символы, точку и тире, чтобы соответствовать `match:john.smith`. +2. Затем идёт `pattern:@` и домен. Это может быть поддомен (например, `host.site.com.uk`), поэтому мы сопоставляем его как слово, за которым следует точка `pattern:([\w-]+\.)` (повторяется). Затем в конце должно быть слово: `match:com` или `match:uk` (но не очень длинное: 2-20 символов). -That regexp is not perfect, but good enough to fix errors or occasional mistypes. +Это выражение не идеальное, но достаточно хорошее для исправления ошибок и опечаток. -For instance, we can find all emails in the string: +Например, мы можем найти все электронные адреса в строке: ```js run let reg = /[-.\w]+@([\w-]+\.)+[\w-]{2,20}/g; @@ -43,17 +43,17 @@ let reg = /[-.\w]+@([\w-]+\.)+[\w-]{2,20}/g; alert("my@mail.com @ his@site.com.uk".match(reg)); // my@mail.com, his@site.com.uk ``` -In this example parentheses were used to make a group for repeating `pattern:(...)+`. But there are other uses too, let's see them. +В этом примере скобки были использованы для создания повторяющейся группы `pattern:(...)+`. Но есть и другие применения. Посмотрим на них. -## Contents of parentheses +## Содержимое скобок -Parentheses are numbered from left to right. The search engine remembers the content of each and allows to reference it in the pattern or in the replacement string. +Скобочные группы нумеруются слева направо. Поисковой движок запоминает содержимое, которое "поймала" каждая группа, и позволяет ссылаться на него в шаблоне регулярного выражения или строке для замены. -For instance, we'd like to find HTML tags `pattern:<.*?>`, and process them. +Например, мы хотим найти HTML теги `pattern:<.*?>` и обработать их. -Let's wrap the inner content into parentheses, like this: `pattern:<(.*?)>`. +Давайте заключим внутреннее содержимое в круглые скобки: `pattern:<(.*?)>`. -We'll get them into an array: +Мы получим как тег целиком, так и его содержимое в виде массива: ```js run let str = '

Hello, world!

'; @@ -62,14 +62,14 @@ let reg = /<(.*?)>/; alert( str.match(reg) ); // Array: ["

", "h1"] ``` -The call to [String#match](mdn:js/String/match) returns groups only if the regexp has no `pattern:/.../g` flag. +Вызов [String#match](mdn:js/String/match) возвращает группы, лишь если регулярное выражение ищет только первое совпадение, то есть не имеет флага `pattern:/.../g`. -If we need all matches with their groups then we can use `.matchAll` or `regexp.exec` as described in : +Если необходимы все совпадения с их группировкой, то мы можем использовать `.matchAll` или `regexp.exec`, как описано в : ```js run let str = '

Hello, world!

'; -// two matches: opening

and closing

tags +// два совпадения: теги открытия

и закрытия

let reg = /<(.*?)>/g; let matches = Array.from( str.matchAll(reg) ); @@ -78,19 +78,19 @@ alert(matches[0]); // Array: ["

", "h1"] alert(matches[1]); // Array: ["

", "/h1"] ``` -Here we have two matches for `pattern:<(.*?)>`, each of them is an array with the full match and groups. +Здесь мы имеем два совпадения для `pattern:<(.*?)>`. Каждое из них является массивом с полным совпадением и группами. -## Nested groups +## Вложенные группы -Parentheses can be nested. In this case the numbering also goes from left to right. +Скобки могут быть и вложенными. В этом случае нумерация также идёт слева направо. -For instance, when searching a tag in `subject:` we may be interested in: +Например, при поиске тега в `subject:` нас может интересовать: -1. The tag content as a whole: `match:span class="my"`. -2. The tag name: `match:span`. -3. The tag attributes: `match:class="my"`. +1. Содержимое тега целиком: `match:span class="my"`. +2. Название тега: `match:span`. +3. Атрибуты тега: `match:class="my"`. -Let's add parentheses for them: +Давайте добавим скобки для них: ```js run let str = ''; @@ -101,51 +101,53 @@ let result = str.match(reg); alert(result); // , span class="my", span, class="my" ``` -Here's how groups look: +Вот так выглядят скобочные группы: ![](regexp-nested-groups.png) -At the zero index of the `result` is always the full match. +По нулевому индексу в `result` всегда идёт полное совпадение. -Then groups, numbered from left to right. Whichever opens first gives the first group `result[1]`. Here it encloses the whole tag content. +Затем следуют группы, нумеруемые слева направо. Группа, которая идёт первой, получает первый индекс в результате -- `result[1]`. Там находится всё содержимое тега. -Then in `result[2]` goes the group from the second opening `pattern:(` till the corresponding `pattern:)` -- tag name, then we don't group spaces, but group attributes for `result[3]`. +Затем в `result[2]` идёт группа, образованная второй открывающей скобкой `pattern:(` до следующей закрывающей скобки `pattern:)` -- имя тега, далее в `result[3]` мы группируем не пробелы, а атрибуты. -**If a group is optional and doesn't exist in the match, the corresponding `result` index is present (and equals `undefined`).** +**Даже если скобочная группа необязательна и не входит в совпадение, соответствующий элемент массива `result` существует (и равен `undefined`).** -For instance, let's consider the regexp `pattern:a(z)?(c)?`. It looks for `"a"` optionally followed by `"z"` optionally followed by `"c"`. +Например, рассмотрим регулярное выражение `pattern:a(z)?(c)?`. Оно ищет букву `"a"`, за которой опционально идёт буква `"z"`, за которой, в свою очередь, опционально идёт буква `"c"`. -If we run it on the string with a single letter `subject:a`, then the result is: +Если применить его к строке из одной буквы `subject:a`, то результат будет такой: ```js run let match = 'a'.match(/a(z)?(c)?/); alert( match.length ); // 3 -alert( match[0] ); // a (whole match) +alert( match[0] ); // a (всё совпадение) alert( match[1] ); // undefined alert( match[2] ); // undefined ``` -The array has the length of `3`, but all groups are empty. +Массив имеет длину `3`, но все скобочные группы пустые. -And here's a more complex match for the string `subject:ack`: +А теперь более сложная ситуация для строки `subject:ack`: ```js run let match = 'ack'.match(/a(z)?(c)?/) alert( match.length ); // 3 -alert( match[0] ); // ac (whole match) -alert( match[1] ); // undefined, because there's nothing for (z)? +alert( match[0] ); // ac (всё совпадение) +alert( match[1] ); // undefined, потому что для (z)? ничего нет alert( match[2] ); // c ``` -The array length is permanent: `3`. But there's nothing for the group `pattern:(z)?`, so the result is `["ac", undefined, "c"]`. +Длина массива всегда равна `3`. Для группы `pattern:(z)?` ничего нет, поэтому результат `["ac", undefined, "c"]`. -## Named groups +## Именованные группы -Remembering groups by their numbers is hard. For simple patterns it's doable, but for more complex ones we can give names to parentheses. +Запоминать группы по номерам не очень удобно. Для простых шаблонов это допустимо, но в более сложных случаях мы можем давать скобкам имена. -That's done by putting `pattern:?` immediately after the opening paren, like this: +Это делается добавлением `pattern:?` непосредственно после открытия скобки. + +Например: ```js run *!* @@ -160,11 +162,11 @@ alert(groups.month); // 04 alert(groups.day); // 30 ``` -As you can see, the groups reside in the `.groups` property of the match. +Как вы можете видеть, группы располагаются в свойстве `.groups` совпадения. -We can also use them in replacements, as `pattern:$` (like `$1..9`, but name instead of a digit). +Мы также можем использовать их в строке замены как `pattern:$` (аналогично `$1..9`, но имя вместо цифры). -For instance, let's rearrange the date into `day.month.year`: +Например, давайте поменяем формат даты в `день.месяц.год`: ```js run let dateRegexp = /(?[0-9]{4})-(?[0-9]{2})-(?[0-9]{2})/; @@ -176,7 +178,7 @@ let rearranged = str.replace(dateRegexp, '$.$.$'); alert(rearranged); // 30.04.2019 ``` -If we use a function, then named `groups` object is always the last argument: +Если используем функцию для замены, тогда именованный объект `groups` всегда является последним аргументом: ```js run let dateRegexp = /(?[0-9]{4})-(?[0-9]{2})-(?[0-9]{2})/; @@ -191,9 +193,9 @@ let rearranged = str.replace(dateRegexp, alert(rearranged); // 30.04.2019 ``` -Usually, when we intend to use named groups, we don't need positional arguments of the function. For the majority of real-life cases we only need `str` and `groups`. +Обычно, когда мы планируем использовать именованные группы, то из всех аргументов функции нам нужны только `str` и` groups`. -So we can write it a little bit shorter: +Так что мы можем написать код чуть короче: ```js let rearranged = str.replace(dateRegexp, (str, ...args) => { @@ -206,20 +208,20 @@ let rearranged = str.replace(dateRegexp, (str, ...args) => { ``` -## Non-capturing groups with ?: +## Исключение из запоминания через ?: -Sometimes we need parentheses to correctly apply a quantifier, but we don't want the contents in results. +Бывает так, что скобки нужны, чтобы квантификатор правильно применился, но мы не хотим, чтобы их содержимое попало в результат. -A group may be excluded by adding `pattern:?:` in the beginning. +Скобочную группу можно исключить из запоминаемых и нумеруемых, добавив в её начало `pattern:?:`. -For instance, if we want to find `pattern:(go)+`, but don't want to remember the contents (`go`) in a separate array item, we can write: `pattern:(?:go)+`. +Например, если мы хотим найти `pattern:(go)+`, но не хотим запоминать содержимое (`go`) в отдельный элемент массива, то можем написать так: `pattern:(?:go)+`. -In the example below we only get the name "John" as a separate member of the `results` array: +В примере ниже мы получаем только имя "John" как отдельный член массива `results`: ```js run let str = "Gogo John!"; *!* -// exclude Gogo from capturing +// исключает Gogo из запоминания let reg = /(?:go)+ (\w+)/i; */!* @@ -229,9 +231,14 @@ alert( result.length ); // 2 alert( result[1] ); // John ``` -## Summary +## Итого + +Круглые скобки группируют вместе часть регулярного выражения, так что квантификатор применяется к ним в целом. + +Скобочные группы нумеруются слева направо и могут опционально именоваться с помощью `(?...)`. + +На текст совпадения, соответствующий скобочной группе, можно ссылаться в строке замены через `$1`, `$2` и т.д. или по имени `$name`, если она именована. + +Часть совпадения, соответствующую скобочной группе, мы также получаем в результатах поиска, отдельным элементом массива (или в `.groups`, если группа именована). -- Parentheses can be: - - capturing `(...)`, ordered left-to-right, accessible by number. - - named capturing `(?...)`, accessible by name. - - non-capturing `(?:...)`, used only to apply quantifier to the whole groups. +Можно исключить скобочную группу из запоминания, добавив в её начало `pattern:?:` -- `(?:...)`. Это используется, если необходимо применить квантификатор ко всей группе, но исключить попадание их содержимого в результат.