This commit is contained in:
Ilya Kantor 2019-06-07 09:29:16 +03:00
parent 6a4c4193ad
commit 3c37e1de7b
2 changed files with 13 additions and 5 deletions

View file

@ -53,9 +53,17 @@ For the future, when you use a browser console to test features, please note tha
Sometimes, when `use strict` makes a difference, you'll get incorrect results. Sometimes, when `use strict` makes a difference, you'll get incorrect results.
Even if we press `key:Shift+Enter` to input multiple lines, and put `use strict` on top, it doesn't work. That's because of how the console executes the code internally. You can try to press `key:Shift+Enter` to input multiple lines, and put `use strict` on top, like this:
The reliable way to ensure `use strict` would be to input the code into console like this: ```js
'use strict'; <Shift+Enter for a newline>
// ...your code
<Enter to run>
```
It works in most browsers, namely Firefox and Chrome.
If it doesn't, the most reliable way to ensure `use strict` would be to input the code into console like this:
```js ```js
(function() { (function() {

View file

@ -1,6 +1,6 @@
# Backreferences in pattern: \n and \k # Backreferences in pattern: \n and \k
Capturing groups can be accessed not only in the result or in the replacement string, but also in the pattern itself. We can use the contents of capturing groups `(...)` not only in the result or in the replacement string, but also in the pattern itself.
## Backreference by number: \n ## Backreference by number: \n
@ -12,7 +12,7 @@ We need to find a quoted string: either a single-quoted `subject:'...'` or a dou
How to look for them? How to look for them?
We can put two kinds of quotes in the pattern: `pattern:['"](.*?)['"]`, but it would find strings with mixed quotes, like `match:"...'` and `match:'..."`. That would lead to incorrect matches when one quote appears inside other ones, like the string `subject:"She's the one!"`: We can put both kinds of quotes in the square brackets: `pattern:['"](.*?)['"]`, but it would find strings with mixed quotes, like `match:"...'` and `match:'..."`. That would lead to incorrect matches when one quote appears inside other ones, like the string `subject:"She's the one!"`:
```js run ```js run
let str = `He said: "She's the one!".`; let str = `He said: "She's the one!".`;
@ -25,7 +25,7 @@ alert( str.match(reg) ); // "She'
As we can see, the pattern found an opening quote `match:"`, then the text is consumed lazily till the other quote `match:'`, that closes the match. As we can see, the pattern found an opening quote `match:"`, then the text is consumed lazily till the other quote `match:'`, that closes the match.
To make sure that the pattern looks for the closing quote exactly the same as the opening one, we can make a groups of it and use the backreference. To make sure that the pattern looks for the closing quote exactly the same as the opening one, we can wrap it into a capturing group and use the backreference.
Here's the correct code: Here's the correct code: