Skip to content

Commit 62c507c

Browse files
committed
ok
1 parent 75e3053 commit 62c507c

File tree

92 files changed

+583
-574
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+583
-574
lines changed

10-regular-expressions-javascript/02-regexp-methods/article.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ alert( result.input ); // "Fame is the thirst of youth" (the string)
4848
4949
The array may have more than one element.
5050
51-
**If a part of the pattern is delimited by brackets `(...)`, then it becomes a separate element of the array.**
51+
**If a part of the pattern is delimited by parentheses `(...)`, then it becomes a separate element of the array.**
5252
5353
For instance:
5454
@@ -58,18 +58,18 @@ lar str = "JavaScript is a programming language";
5858
let result = str.match( *!*/JAVA(SCRIPT)/i*/!* );
5959

6060
alert( result[0] ); // JavaScript (the whole match)
61-
alert( result[1] ); // script (the part of the match that corresponds to the brackets)
61+
alert( result[1] ); // script (the part of the match that corresponds to the parentheses)
6262
alert( result.index ); // 0
6363
alert( result.input ); // JavaScript is a programming language
6464
```
6565
6666
Due to the `i` flag the search is case-insensitive, so it finds `match:JavaScript`. The part of the match that corresponds to `pattern:SCRIPT` becomes a separate array item.
6767
68-
We'll be back to brackets later in the chapter [todo]. They are great for search-and-replace.
68+
We'll be back to parentheses later in the chapter <info:regexp-groups>. They are great for search-and-replace.
6969
7070
## str.match(reg) with "g" flag
7171
72-
When there's a `"g"` flag, then `str.match` returns an array of all matches. There are no additional properties in that array, and brackets do not create any elements.
72+
When there's a `"g"` flag, then `str.match` returns an array of all matches. There are no additional properties in that array, and parentheses do not create any elements.
7373
7474
For instance:
7575
@@ -81,9 +81,7 @@ let result = str.match( *!*/ho/ig*/!* );
8181
alert( result ); // HO, Ho, ho (all matches, case-insensitive)
8282
```
8383
84-
With brackets nothing changes, here we go:
85-
86-
84+
With parentheses nothing changes, here we go:
8785
8886
```js run
8987
let str = "HO-Ho-ho!";
@@ -95,7 +93,7 @@ alert( result ); // HO, Ho, ho
9593
9694
So, with `g` flag the `result` is a simple array of matches. No additional properties.
9795
98-
If we want to get information about match positions and use brackets then we should use [RegExp#exec](mdn:js/RegExp/exec) method that we'll cover below.
96+
If we want to get information about match positions and use parentheses then we should use [RegExp#exec](mdn:js/RegExp/exec) method that we'll cover below.
9997
10098
````warn header="If there are no matches, the call to `match` returns `null`"
10199
Please note, that's important. If there were no matches, the result is not an empty array, but `null`.
@@ -155,7 +153,7 @@ We can use special characters in it:
155153
|`$&`|the whole match|
156154
|<code>$&#096;</code>|a part of the string before the match|
157155
|`$'`|a part of the string after the match|
158-
|`$n`|if `n` is a 1-2 digit number, then it means the contents of n-th brackets counting fro left to right|
156+
|`$n`|if `n` is a 1-2 digit number, then it means the contents of n-th parentheses counting fro left to right|
159157
160158
For instance let's use `$&` to replace all entries of `"John"` by `"Mr.John"`:
161159

@@ -167,7 +165,7 @@ alert(str.replace(/John/g, 'Mr.$&'));
167165
// "Mr.John Doe, Mr.John Smith and Mr.John Bull.";
168166
```
169167

170-
Brackets are very often used together with `$1`, `$2`, like this:
168+
Parentheses are very often used together with `$1`, `$2`, like this:
171169

172170
```js run
173171
let str = "John Smith";
@@ -195,11 +193,11 @@ In the example above the function just returns the next number every time, but u
195193
The function is called with arguments `func(str, p1, p2, ..., pn, offset, s)`:
196194

197195
1. `str` -- the match,
198-
2. `p1, p2, ..., pn` -- contents of brackets (if there are any),
196+
2. `p1, p2, ..., pn` -- contents of parentheses (if there are any),
199197
3. `offset` -- position of the match,
200198
4. `s` -- the source string.
201199

202-
If there are no brackets in the regexp, then the function always has 3 arguments: `func(str, offset, s)`.
200+
If there are no parentheses in the regexp, then the function always has 3 arguments: `func(str, offset, s)`.
203201

204202
Let's use it to show full information about matches:
205203

@@ -219,11 +217,11 @@ alert( 'Result: ' + result ); // Result: ho-ho-ho
219217
// Found ho at position 6 in string HO-Ho-ho
220218
```
221219
222-
In the example below there are two brackets, so `replacer` is called with 5 arguments: `str` is the full match, then brackets, and then `offset` and `s`:
220+
In the example below there are two parentheses, so `replacer` is called with 5 arguments: `str` is the full match, then parentheses, and then `offset` and `s`:
223221
224222
```js run
225223
function replacer(str, name, surname, offset, s) {
226-
// name is the first bracket, surname is the second one
224+
// name is the first parentheses, surname is the second one
227225
return surname + ", " + name;
228226
}
229227

@@ -264,10 +262,10 @@ alert( str.search(*!*/love/i*/!*) != -1 ); // false
264262
We've already seen these searching methods:
265263
266264
- `search` -- looks for the position of the match,
267-
- `match` -- if there's no `g` flag, returns the first match with brackets,
268-
- `match` -- if there's a `g` flag -- returns all matches, without separating brackets.
265+
- `match` -- if there's no `g` flag, returns the first match with parentheses,
266+
- `match` -- if there's a `g` flag -- returns all matches, without separating parentheses.
269267
270-
The `regexp.exec` method is a bit harder to use, but it allows to search all matches with brackets and positions.
268+
The `regexp.exec` method is a bit harder to use, but it allows to search all matches with parentheses and positions.
271269
272270
It behaves differently depending on whether the regexp has the `g` flag.
273271
@@ -276,7 +274,7 @@ It behaves differently depending on whether the regexp has the `g` flag.
276274
277275
As we can see, the method gives us nothing new if we use it without the `g` flag, because `str.match` does exactly the same.
278276
279-
But the `g` flag allows to get all matches with their positions and bracket groups.
277+
But the `g` flag allows to get all matches with their positions and parentheses groups.
280278
281279
Here's the example how subsequent `regexp.exec` calls return matches one by one:
282280
@@ -316,7 +314,7 @@ alert( matchThree ); // null (no match)
316314
alert( regexp.lastIndex ); // 0 (reset)
317315
```
318316
319-
As we can see, each `regexp.exec` call returns the match in a "full format": as an array with brackets, `index` and `input` properties.
317+
As we can see, each `regexp.exec` call returns the match in a "full format": as an array with parentheses, `index` and `input` properties.
320318
321319
The main use case for `regexp.exec` is to find all matches in a loop:
322320

10-regular-expressions-javascript/04-regexp-character-sets-and-ranges/2-find-time-2-formats/solution.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

10-regular-expressions-javascript/04-regexp-character-sets-and-ranges/2-find-time-2-formats/task.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

10-regular-expressions-javascript/035-regexp-escaping/article.md renamed to 10-regular-expressions-javascript/04-regexp-escaping/article.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
# Special characters
2+
# Escaping, special characters
33

44
As we've seen, a backslash `"\"` is used to denote character classes. So it's a special character.
55

@@ -21,7 +21,7 @@ For instance, we need to find a dot `pattern:'.'`. In a regular expression a dot
2121
alert( "Chapter 5.1".match(/\d\.\d/) ); // 5.1
2222
```
2323

24-
Brackets are also special characters, so if we want them, we should use `pattern:\(`. The example below looks for a string `"g()"`:
24+
Parentheses are also special characters, so if we want them, we should use `pattern:\(`. The example below looks for a string `"g()"`:
2525

2626
```js run
2727
alert( "function g()".match(/g\(\)/) ); // "g()"
@@ -46,5 +46,46 @@ alert( "/".match(/\//) ); // '/'
4646
From the other hand, the alternative `new RegExp` syntaxes does not require escaping it:
4747

4848
```js run
49-
alert( "/".match(new RegExp()/\//) ); // '/'
49+
alert( "/".match(new RegExp("/")) ); // '/'
50+
```
51+
52+
## new RegExp
53+
54+
If we are creating a regular expression with `new RegExp`, then we need to do some more escaping.
55+
56+
For instance, consider this:
57+
58+
```js run
59+
let reg = new RegExp("\d\.\d");
60+
61+
alert( "Chapter 5.1".match(reg) ); // null
62+
```
63+
64+
It doesn't work, but why?
65+
66+
The reason is string escaping rules. Look here:
67+
68+
```js run
69+
alert("\d\.\d"); // d.d
70+
```
71+
72+
Backslashes are used for escaping inside a string and string-specific special characters like `\n`. The quotes "consume" and interpret them, for instance:
73+
74+
- `\n` -- becomes a newline character,
75+
- `\u1234` -- becomes the Unicode character with such code,
76+
- ...And when there's no special meaning: like `\d` or `\z`, then the backslash is simply removed.
77+
78+
So the call to `new RegExp` gets a string without backslashes.
79+
80+
To fix it, we need to double backslashes, because quotes turn `\\` into `\`:
81+
82+
```js run
83+
*!*
84+
let regStr = "\\d\\.\\d";
85+
*/!*
86+
alert(regStr); // \d\.\d (correct now)
87+
88+
let reg = new RegExp(regStr);
89+
90+
alert( "Chapter 5.1".match(reg) ); // 5.1
5091
```

10-regular-expressions-javascript/04-regexp-character-sets-and-ranges/1-find-range-1/solution.md renamed to 10-regular-expressions-javascript/05-regexp-character-sets-and-ranges/1-find-range-1/solution.md

File renamed without changes.

10-regular-expressions-javascript/04-regexp-character-sets-and-ranges/1-find-range-1/task.md renamed to 10-regular-expressions-javascript/05-regexp-character-sets-and-ranges/1-find-range-1/task.md

File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Answer: `pattern:\d\d[-:]\d\d`.
2+
3+
```js run
4+
let reg = /\d\d[-:]\d\d/g;
5+
alert( "Breakfast at 09:00. Dinner at 21-30".match(reg) ); // 09:00, 21-30
6+
```
7+
8+
Please note that the dash `pattern:'-'` has a special meaning in square brackets, but only between other characters, not when it's in the beginning or at the end, so we don't need to escape it.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Find the time as hh:mm or hh-mm
2+
3+
The time can be in the format `hours:minutes` or `hours-minutes`. Both hours and minutes have 2 digits: `09:00` or `21-30`.
4+
5+
Write a regexp to find time:
6+
7+
```js
8+
let reg = /your regexp/g;
9+
alert( "Breakfast at 09:00. Dinner at 21-30".match(reg) ); // 09:00, 21-30
10+
```
11+
12+
P.S. In this task we assume that the time is always correct, there's no need to filter out bad strings like "45:67". Later we'll deal with that too.

10-regular-expressions-javascript/04-regexp-character-sets-and-ranges/article.md renamed to 10-regular-expressions-javascript/05-regexp-character-sets-and-ranges/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ In square brackets the vast majority of special characters can be used without e
8888

8989
- A dot `pattern:'.'`.
9090
- A plus `pattern:'+'`.
91-
- Brackets `pattern:'( )'`.
91+
- Parentheses `pattern:'( )'`.
9292
- Dash `pattern:'-'` in the beginning or the end (where it does not define a range).
9393
- A caret `pattern:'^'` if not in the beginning (where it means exclusion).
9494
- And the opening square bracket `pattern:'['`.

10-regular-expressions-javascript/06-regexp-quantifiers/2-find-html-colors-6hex/solution.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)