This commit is contained in:
Ilya Kantor 2019-06-03 16:53:46 +03:00
parent c4593f1a60
commit c92f626701
6 changed files with 18 additions and 16 deletions

View file

@ -1,14 +1,14 @@
Opening tag is `pattern:\[(b|url|quote)\]`.
Then to find everything till the closing tag -- let's the pattern `pattern:[\s\S]*?` to match any character including the newline and then a backreference to the closing tag.
Then to find everything till the closing tag -- let's use the pattern `pattern:.*?` with flag `s` to match any character including the newline and then add a backreference to the closing tag.
The full pattern: `pattern:\[(b|url|quote)\][\s\S]*?\[/\1\]`.
The full pattern: `pattern:\[(b|url|quote)\].*?\[/\1\]`.
In action:
```js run
let reg = /\[(b|url|quote)\][\s\S]*?\[\/\1\]/g;
let reg = /\[(b|url|quote)\].*?\[\/\1\]/gs;
let str = `
[b]hello![/b]

View file

@ -32,7 +32,7 @@ Create a regexp to find all BB-tags with their contents.
For instance:
```js
let reg = /your regexp/g;
let reg = /your regexp/flags;
let str = "..[url]http://google.com[/url]..";
alert( str.match(reg) ); // [url]http://google.com[/url]
@ -41,7 +41,7 @@ alert( str.match(reg) ); // [url]http://google.com[/url]
If tags are nested, then we need the outer tag (if we want we can continue the search in its content):
```js
let reg = /your regexp/g;
let reg = /your regexp/flags;
let str = "..[url][b]http://google.com[/b][/url]..";
alert( str.match(reg) ); // [url][b]http://google.com[/b][/url]

View file

@ -2,15 +2,15 @@
Create a regexp to find strings in double quotes `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:\\`.
The strings should support escaping, 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
let str = "Just like \"here\".";
```
For us it's important that an escaped quote `subject:\"` does not end a string.
Please note, in particular, 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.
So we should search 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.

View file

@ -31,7 +31,7 @@ To separate a part of the pattern for alternation we usually enclose it in paren
## Regexp for time
In previous chapters there was a task to build a regexp for searching time in the form `hh:mm`, for instance `12:00`. But a simple `pattern:\d\d:\d\d` is too vague. It accepts `25:99` as the time (99 seconds is valid, but shouldn't be).
In previous chapters there was a task to build a regexp for searching time in the form `hh:mm`, for instance `12:00`. But a simple `pattern:\d\d:\d\d` is too vague. It accepts `25:99` as the time (as 99 seconds match the pattern).
How can we make a better one?