From ad46ef780edabfd33dbc7550c2080c7d37fcd813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D1=81=D0=B8=D0=BB=D0=B8=D0=B9?= Date: Wed, 21 Jul 2021 23:46:16 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D1=83=D1=82=D0=BE=D1=87=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=84=D0=BE=D1=80=D0=BC=D1=83=D0=BB=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D1=83=20=D0=B8=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../03-match-quoted-string/solution.md | 4 ++-- .../03-match-quoted-string/task.md | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md b/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md index f74041cb26..714cafa438 100644 --- a/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md +++ b/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md @@ -11,7 +11,7 @@ ```js run let regexp = /"(\\.|[^"\\])*"/g; -let str = ' .. "test me" .. "Скажи \\"Привет\\"!" .. "\\\\ \\"" .. '; +let str = ' .. "test me" .. "Скажи \\"Привет\\"!" .. \\"_" .. "\\\\ \\"" .. '; -alert( str.match(regexp) ); // "test me","Скажи \"Привет\"!","\\ \"" +alert( str.match(regexp) ); // "test me","Скажи \"Привет\"!","_","\\ \"" ``` diff --git a/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/task.md b/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/task.md index 0d28a3566a..5fa080f091 100644 --- a/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/task.md +++ b/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/task.md @@ -7,10 +7,16 @@ ```js let str = "Как вот \"здесь\"."; ``` +Причём экранирование работает только внутри строк. -В частности, обратите внимание: двойная кавычка после обратного слеша `subject:\"` не оканчивает строку. +Поэтому двойная кавычка после обратного слеша `subject:\"` не оканчивает строку, но может начинать её: -Поэтому мы должны искать от одной кавычки до другой, игнорируя встречающиеся экранированные кавычки. +```js +..\*!*"Строка1"*/!* aaa \*!*"Строка2"*/!*.. // Верно +..\"Строка1*!*" aaa \"Строка2"*/!*.. // Неверно +``` + +Следовательно, мы должны искать от одной кавычки до другой, игнорируя встречающиеся между ними экранированные кавычки. В этом и состоит основная сложность задачи, которая без этого условия была бы элементарной. @@ -18,15 +24,15 @@ let str = "Как вот \"здесь\"."; ```js .. *!*"test me"*/!* .. .. *!*"Скажи \"Привет\"!"*/!* ... (строка с экранированными кавычками) -.. *!*"\\"*/!* .. (внутри двойной слеш) -.. *!*"\\ \""*/!* .. (внутри двойной слеш и экранированная кавычка) +..\*!*" ^_^ "*/!* .. (вне строк экранирование не работает) +.. *!*"\\ \""*/!* .. (внутри двойной слеш и экранированная кавычка) ``` В JavaScript приходится удваивать обратные слеши, чтобы добавлять их в строку, как здесь: ```js run -let str = ' .. "test me" .. "Скажи \\"Привет\\"!" .. "\\\\ \\"" .. '; +let str = ' .. "test me" .. "Скажи \\"Привет\\"!" .. \\"_" .. "\\\\ \\"" .. '; // эта строка в памяти: -alert(str); // .. "test me" .. "Скажи \"Привет\"!" .. "\\ \"" .. +alert(str); // .. "test me" .. "Скажи \"Привет\"!" .. \"_" .. "\\ \"" .. ``` From 4c6bea05dccaa38e6c854ff56383f6e681445d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D1=81=D0=B8=D0=BB=D0=B8=D0=B9?= Date: Tue, 9 Nov 2021 21:56:23 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D0=B1=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B4=D0=BB=D0=B8=D0=BD=D0=BD=D1=83=D1=8E=20=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D0=BA=D1=83-=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B4=D0=B2=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../03-match-quoted-string/solution.md | 7 +++++-- .../13-regexp-alternation/03-match-quoted-string/task.md | 8 +++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md b/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md index 714cafa438..ea57cf1ab1 100644 --- a/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md +++ b/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/solution.md @@ -11,7 +11,10 @@ ```js run let regexp = /"(\\.|[^"\\])*"/g; -let str = ' .. "test me" .. "Скажи \\"Привет\\"!" .. \\"_" .. "\\\\ \\"" .. '; -alert( str.match(regexp) ); // "test me","Скажи \"Привет\"!","_","\\ \"" +let str1 = ' .. "test me" .. "Скажи \\"Привет\\"!" .. ' +let str2 = ' .. \\" ^_^ " .. "\\\\ \\"" .. '; + +alert( str1.match(regexp) ); // "test me", "Скажи \"Привет\"!" +alert( str2.match(regexp) ); // " ^_^ ", "\\ \"" ``` diff --git a/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/task.md b/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/task.md index 5fa080f091..99afb35d92 100644 --- a/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/task.md +++ b/9-regular-expressions/13-regexp-alternation/03-match-quoted-string/task.md @@ -31,8 +31,10 @@ let str = "Как вот \"здесь\"."; В JavaScript приходится удваивать обратные слеши, чтобы добавлять их в строку, как здесь: ```js run -let str = ' .. "test me" .. "Скажи \\"Привет\\"!" .. \\"_" .. "\\\\ \\"" .. '; +let str1 = ' .. "test me" .. "Скажи \\"Привет\\"!" .. '; +let str2 = ' .. \\" ^_^ " .. "\\\\ \\"" .. '; -// эта строка в памяти: -alert(str); // .. "test me" .. "Скажи \"Привет\"!" .. \"_" .. "\\ \"" .. +// эти строки в памяти: +alert(str1); // .. "test me" .. "Скажи \"Привет\"!" .. +alert(str2); // .. \" ^_^ " .. "\\ \"" .. ```