regexp
This commit is contained in:
parent
20547570ff
commit
681cae4b6a
16 changed files with 505 additions and 362 deletions
|
@ -18,7 +18,7 @@ let str = "First HTML appeared, then CSS, then JavaScript";
|
|||
alert( str.match(reg) ); // 'HTML', 'CSS', 'JavaScript'
|
||||
```
|
||||
|
||||
We already know a similar thing -- square brackets. They allow to choose between multiple character, for instance `pattern:gr[ae]y` matches `match:gray` or `match:grey`.
|
||||
We already saw a similar thing -- square brackets. They allow to choose between multiple characters, for instance `pattern:gr[ae]y` matches `match:gray` or `match:grey`.
|
||||
|
||||
Square brackets allow only characters or character sets. Alternation allows any expressions. A regexp `pattern:A|B|C` means one of expressions `A`, `B` or `C`.
|
||||
|
||||
|
@ -27,30 +27,41 @@ For instance:
|
|||
- `pattern:gr(a|e)y` means exactly the same as `pattern:gr[ae]y`.
|
||||
- `pattern:gra|ey` means `match:gra` or `match:ey`.
|
||||
|
||||
To separate a part of the pattern for alternation we usually enclose it in parentheses, like this: `pattern:before(XXX|YYY)after`.
|
||||
To apply alternation to a chosen part of the pattern, we can enclose it in parentheses:
|
||||
- `pattern:I love HTML|CSS` matches `match:I love HTML` or `match:CSS`.
|
||||
- `pattern:I love (HTML|CSS)` matches `match:I love HTML` or `match:I love CSS`.
|
||||
|
||||
## Regexp for time
|
||||
## Example: 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 (as 99 seconds match the pattern).
|
||||
In previous articles 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, but that time is invalid).
|
||||
|
||||
How can we make a better one?
|
||||
How can we make a better pattern?
|
||||
|
||||
We can apply more careful matching. First, the hours:
|
||||
We can use more careful matching. First, the hours:
|
||||
|
||||
- If the first digit is `0` or `1`, then the next digit can by anything.
|
||||
- Or, if the first digit is `2`, then the next must be `pattern:[0-3]`.
|
||||
- If the first digit is `0` or `1`, then the next digit can be any: `pattern:[01]\d`.
|
||||
- Otherwise, if the first digit is `2`, then the next must be `pattern:[0-3]`.
|
||||
- (no other first digit is allowed)
|
||||
|
||||
As a regexp: `pattern:[01]\d|2[0-3]`.
|
||||
We can write both variants in a regexp using alternation: `pattern:[01]\d|2[0-3]`.
|
||||
|
||||
Next, the minutes must be from `0` to `59`. In the regexp language that means `pattern:[0-5]\d`: the first digit `0-5`, and then any digit.
|
||||
Next, minutes must be from `00` to `59`. In the regular expression language that can be written as `pattern:[0-5]\d`: the first digit `0-5`, and then any digit.
|
||||
|
||||
Let's glue them together into the pattern: `pattern:[01]\d|2[0-3]:[0-5]\d`.
|
||||
If we glue minutes and seconds together, we get the pattern: `pattern:[01]\d|2[0-3]:[0-5]\d`.
|
||||
|
||||
We're almost done, but there's a problem. The alternation `pattern:|` now happens to be between `pattern:[01]\d` and `pattern:2[0-3]:[0-5]\d`.
|
||||
|
||||
That's wrong, as it should be applied only to hours `[01]\d` OR `2[0-3]`. That's a common mistake when starting to work with regular expressions.
|
||||
That is: minutes are added to the second alternation variant, here's a clear picture:
|
||||
|
||||
The correct variant:
|
||||
```
|
||||
[01]\d | 2[0-3]:[0-5]\d
|
||||
```
|
||||
|
||||
That pattern looks for `pattern:[01]\d` or `pattern:2[0-3]:[0-5]\d`.
|
||||
|
||||
But that's wrong, the alternation should only be used in the "hours" part of the regular expression, to allow `pattern:[01]\d` OR `pattern:2[0-3]`. Let's correct that by enclosing "hours" into parentheses: `pattern:([01]\d|2[0-3]):[0-5]\d`.
|
||||
|
||||
The final solution:
|
||||
|
||||
```js run
|
||||
let reg = /([01]\d|2[0-3]):[0-5]\d/g;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue