Skip to content

Commit 6384ccb

Browse files
committed
minor fixes
1 parent b5ea34c commit 6384ccb

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

1-js/05-data-types/07-map-set/article.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ alert( map.size ); // 3
4141

4242
As we can see, unlike objects, keys are not converted to strings. Any type of key is possible.
4343

44+
```smart header="`map[key]` isn't the right way to use a `Map`"
45+
Although `map[key]` also works, e.g. we can set `map[key] = 2`, this is treating `map` as a plain JavaScript object, so it implies all corresponding limitations (no object keys and so on).
46+
47+
So we should use `map` methods: `set`, `get` and so on.
48+
```
49+
4450
**Map can also use objects as keys.**
4551
4652
For instance:
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
In order to insert after the `<body>` tag, you must first find it. We will use the regular expression pattern `pattern:<body.*>`.
1+
In order to insert after the `<body>` tag, we must first find it. We can use the regular expression pattern `pattern:<body.*>` for that.
22

3-
Next, we need to leave the `<body>` tag in place and add text after it.
3+
In this task we don't need to modify the `<body>` tag. We only need to add the text after it.
4+
5+
Here's how we can do it:
46

5-
This can be done like this:
67
```js run
78
let str = '...<body style="...">...';
89
str = str.replace(/<body.*>/, '$&<h1>Hello</h1>');
910

1011
alert(str); // ...<body style="..."><h1>Hello</h1>...
1112
```
1213

13-
In the replacement string `$&` means the match itself, that is, we replace `pattern:<body.*>` Is replaced by itself plus `<h1>Hello</h1>`.
14+
In the replacement string `$&` means the match itself, that is, the part of the source text that corresponds to `pattern:<body.*>`. It gets replaced by itself plus `<h1>Hello</h1>`.
1415

15-
An alternative is to use retrospective validation:
16+
An alternative is to use lookbehind:
1617

1718
```js run
1819
let str = '...<body style="...">...';
@@ -21,8 +22,15 @@ str = str.replace(/(?<=<body.*>)/, `<h1>Hello</h1>`);
2122
alert(str); // ...<body style="..."><h1>Hello</h1>...
2223
```
2324

24-
Such a regular expression at each position will check if `pattern:<body.*>`does not go directly in front of it. If yes, a match is found. But the tag `pattern:<body.*>` does not coincide, it only participates in the verification. And there are no other characters after checking in it, so the match text will be empty.
25+
As you can see, there's only lookbehind part in this regexp.
26+
27+
It works like this:
28+
- At every position in the text.
29+
- Check if it's preceeded by `pattern:<body.*>`.
30+
- If it's so then we have the match.
31+
32+
The tag `pattern:<body.*>` won't be returned. The result of this regexp is literally an empty string, but it matches only at positions preceeded by `pattern:<body.*>`.
2533

26-
This replaces the "empty line", followed by `pattern:<body.*>` With `<h1>Hello</h1>`. Which, exactly, is the insertion of this line after `<body>`.
34+
So we replaces the "empty line", preceeded by `pattern:<body.*>`, with `<h1>Hello</h1>`. That's the insertion after `<body>`.
2735

28-
P.S. The flags: `pattern:/<body.*>/si`, will not interfere with this regular expression, so that a line break appears in the "dot" (a tag can span several lines), and also that the tags are in a different register of the `match:<BODY>` type, too.
36+
P.S. Regexp flags, such as `pattern:s` and `pattern:i` can also useful: `pattern:/<body.*>/si`. The `pattern:s` flag makes the dot `pattern:.` match a newline character, and `pattern:i` flag makes `pattern:<body>` also match `match:<BODY>` case-insensitively.

9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Insert After Head
22

3-
There is a line with an HTML Document.
3+
We have a string with an HTML Document.
44

5-
Insert after tag `<body>` (it may have attributes) line `<h1>Hello</h1>`.
5+
Write a regular expression that inserts `<h1>Hello</h1>` immediately after `<body>` tag. The tag may have attributes.
66

77
For instance:
88

@@ -20,7 +20,7 @@ let str = `
2020
str = str.replace(regexp, `<h1>Hello</h1>`);
2121
```
2222

23-
After that value `str`:
23+
After that the value of `str` should be:
2424
```html
2525
<html>
2626
<body style="height: 200px"><h1>Hello</h1>

0 commit comments

Comments
 (0)