This commit is contained in:
Ilya Kantor 2017-03-20 20:52:29 +03:00
parent 1e2b09b6fb
commit 7ddea43ab4
22 changed files with 382 additions and 343 deletions

View file

@ -1,17 +1,17 @@
Решение задачи: `pattern:/"(\\.|[^"\\])*"/g`.
The solution: `pattern:/"(\\.|[^"\\])*"/g`.
То есть:
Step by step:
- Сначала ищем кавычку `pattern:"`
- Затем, если далее слэш `pattern:\\` (удвоение слэша -- техническое, для вставки в регэксп, на самом деле там один слэш), то после него также подойдёт любой символ (точка).
- Если не слэш, то берём любой символ, кроме кавычек (которые будут означать конец строки) и слэша (чтобы предотвратить одинокие слэши, сам по себе единственный слэш не нужен, он должен экранировать какой-то символ) `pattern:[^"\\]`
- ...И так жадно, до закрывающей кавычки.
- First we look for an opening quote `pattern:"`
- Then if we have a backslash `pattern:\\` (we technically have to double it in the pattern, because it is a special character, so that's a single backslash in fact), then any character is fine after it (a dot).
- Otherwise we take any character except a quote (that would mean the end of the string) and a backslash (to prevent lonely backslashes, the backslash is only used with some other symbol after it): `pattern:[^"\\]`
- ...And so on till the closing quote.
В действии:
In action:
```js run
var re = /"(\\.|[^"\\])*"/g;
var str = '.. "test me" .. "Скажи \\"Привет\\"!" .. "\\r\\n\\\\" ..';
let reg = /"(\\.|[^"\\])*"/g;
let str = ' .. "test me" .. "Say \\"Hello\\"!" .. "\\\\ \\"" .. ';
alert( str.match(re) ); // "test me","Скажи \"Привет\"!","\r\n\\"
alert( str.match(reg) ); // "test me","Say \"Hello\"!","\\ \""
```

View file

@ -1,25 +1,32 @@
# Найдите строки в кавычках
# Find quoted strings
Найдите в тексте при помощи регэкспа строки в двойных кавычках `subject:"..."`.
Create a regexp to find strings in double quotes `subject:"..."`.
В строке поддерживается экранирование при помощи слеша -- примерно в таком же виде, как в обычных строках JavaScript. То есть, строка может содержать любые символы, экранированные слэшем, в частности: `subject:\"`, `subject:\n`, и даже сам слэш в экранированном виде: `subject:\\`.
The important part is that strings should support escaping, in the same way as JavaScript strings do. For instance, quotes can be inserted as `subject:\"` a newline as `subject:\n`, and the slash itself as `subject:\\`.
Здесь особо важно, что двойная кавычка после слэша не оканчивает строку, а считается её частью. В этом и состоит основная сложность задачи, которая без этого условия была бы элементарной.
Пример совпадающих строк:
```js
.. *!*"test me"*/!* .. (обычная строка)
.. *!*"Скажи \"Привет\"!"*/!* ... (строка с кавычками внутри)
.. *!*"\r\n\\"*/!* .. (строка со спец. символами и слэшем внутри)
let str = "Just like \"here\".";
```
Заметим, что в JavaScript такие строки удобнее всего задавать в одинарных кавычках, и слеши придётся удвоить (в одинарных кавычках они являются экранирующими символами):
For us it's important that an escaped quote `subject:\"` does not end a string.
So we should look from one quote to the other ignoring escaped quotes on the way.
That's the essential part of the task, otherwise it would be trivial.
Examples of strings to match:
```js
.. *!*"test me"*/!* ..
.. *!*"Say \"Hello\"!"*/!* ... (escaped quotes inside)
.. *!*"\\"*/!* .. (double slash inside)
.. *!*"\\ \""*/!* .. (double slash and an escaped quote inside)
```
In JavaScript we need to double the slashes to pass them right into the string, like this:
Пример задания тестовой строки в JavaScript:
```js run
var str = ' .. "test me" .. "Скажи \\"Привет\\"!" .. "\\r\\n\\\\" .. ';
let str = ' .. "test me" .. "Say \\"Hello\\"!" .. "\\\\ \\"" .. ';
// эта строка будет такой:
alert(str); // .. "test me" .. "Скажи \"Привет\"!" .. "\r\n\\" ..
// the in-memory string
alert(str); // .. "test me" .. "Say \"Hello\"!" .. "\\ \"" ..
```