fixes
This commit is contained in:
parent
c4593f1a60
commit
c92f626701
6 changed files with 18 additions and 16 deletions
|
@ -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]
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -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?
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue