diff --git a/1-js/05-data-types/07-map-set/article.md b/1-js/05-data-types/07-map-set/article.md index c4d7c21a..aaed5b45 100644 --- a/1-js/05-data-types/07-map-set/article.md +++ b/1-js/05-data-types/07-map-set/article.md @@ -41,6 +41,12 @@ alert( map.size ); // 3 As we can see, unlike objects, keys are not converted to strings. Any type of key is possible. +```smart header="`map[key]` isn't the right way to use a `Map`" +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). + +So we should use `map` methods: `set`, `get` and so on. +``` + **Map can also use objects as keys.** For instance: diff --git a/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/solution.md b/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/solution.md index fffada18..b5915744 100644 --- a/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/solution.md +++ b/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/solution.md @@ -1,8 +1,9 @@ -In order to insert after the `` tag, you must first find it. We will use the regular expression pattern `pattern:`. +In order to insert after the `` tag, we must first find it. We can use the regular expression pattern `pattern:` for that. -Next, we need to leave the `` tag in place and add text after it. +In this task we don't need to modify the `` tag. We only need to add the text after it. + +Here's how we can do it: -This can be done like this: ```js run let str = '......'; str = str.replace(//, '$&

Hello

'); @@ -10,9 +11,9 @@ str = str.replace(//, '$&

Hello

'); alert(str); // ...

Hello

... ``` -In the replacement string `$&` means the match itself, that is, we replace `pattern:` Is replaced by itself plus `

Hello

`. +In the replacement string `$&` means the match itself, that is, the part of the source text that corresponds to `pattern:`. It gets replaced by itself plus `

Hello

`. -An alternative is to use retrospective validation: +An alternative is to use lookbehind: ```js run let str = '......'; @@ -21,8 +22,15 @@ str = str.replace(/(?<=)/, `

Hello

`); alert(str); // ...

Hello

... ``` -Such a regular expression at each position will check if `pattern:`does not go directly in front of it. If yes, a match is found. But the tag `pattern:` 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. +As you can see, there's only lookbehind part in this regexp. -This replaces the "empty line", followed by `pattern:` With `

Hello

`. Which, exactly, is the insertion of this line after ``. +It works like this: +- At every position in the text. +- Check if it's preceeded by `pattern:`. +- If it's so then we have the match. -P.S. The flags: `pattern://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:` type, too. +The tag `pattern:` won't be returned. The result of this regexp is literally an empty string, but it matches only at positions preceeded by `pattern:`. + +So we replaces the "empty line", preceeded by `pattern:`, with `

Hello

`. That's the insertion after ``. + +P.S. Regexp flags, such as `pattern:s` and `pattern:i` can also useful: `pattern://si`. The `pattern:s` flag makes the dot `pattern:.` match a newline character, and `pattern:i` flag makes `pattern:` also match `match:` case-insensitively. diff --git a/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/task.md b/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/task.md index 9ecc2a7a..be1a259f 100644 --- a/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/task.md +++ b/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/task.md @@ -1,8 +1,8 @@ # Insert After Head -There is a line with an HTML Document. +We have a string with an HTML Document. -Insert after tag `` (it may have attributes) line `

Hello

`. +Write a regular expression that inserts `

Hello

` immediately after `` tag. The tag may have attributes. For instance: @@ -20,7 +20,7 @@ let str = ` str = str.replace(regexp, `

Hello

`); ``` -After that value `str`: +After that the value of `str` should be: ```html

Hello