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 b5915744..f2bbd3f5 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,4 +1,4 @@ -In order to insert after the `` tag, we must first find it. We can use the regular expression pattern `pattern:` for that. +In order to insert after the `` tag, we must first find it. We can use the regular expression pattern `pattern:` for that. In this task we don't need to modify the `` tag. We only need to add the text after it. @@ -6,18 +6,18 @@ Here's how we can do it: ```js run let str = '......'; -str = str.replace(//, '$&

Hello

'); +str = str.replace(//, '$&

Hello

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

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

`. +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 lookbehind: ```js run let str = '......'; -str = str.replace(/(?<=)/, `

Hello

`); +str = str.replace(/(?<=)/, `

Hello

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

Hello

... ``` @@ -26,11 +26,11 @@ As you can see, there's only lookbehind part in this regexp. It works like this: - At every position in the text. -- Check if it's preceeded by `pattern:`. +- Check if it's preceeded by `pattern:`. - If it's so then we have the match. -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:`. +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 ``. +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. +P.S. Regexp flags, such as `pattern:s` and `pattern:i` can also be 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.