◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
2 changes: 1 addition & 1 deletion 1-js/06-advanced-functions/01-recursion/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ list.next = list.next.next;

当然,链表不总是优于数组。不然大家都去使用链表了。

主要的不足就是我们无法轻易通过它的编号获取元素。在数组中却很容易:`arr[n]` 是一个直接引用。而在列表中,我们需要从起点元素顺着 `next` 找 `N` 次才能获取到第 N 个元素。

...但是我们并不总需要这样的操作。比如,当我们需要一个队列或者甚至一个[双向队列](https://en.wikipedia.org/wiki/Double-ended_queue) —— 有序结构必须可以快速的从两端添加、移除元素。

Expand Down
2 changes: 1 addition & 1 deletion 1-js/10-error-handling/1-try-catch/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ try {
- `name` —— 异常名称(异常对象的构造函数的名称)。
- `stack`(没有标准) —— 异常发生时的调用栈。

我们也可以通过使用 `throw` 运算符来生成自定义的异常。技术上来讲,`throw` 的参数没有限制,但是通常它是一个继承自内置的 `Error` 类的异常对象。更对关于异常的扩展,请看下个章节。

重新抛出异常,是一种异常处理的基本模式:`catch` 代码块通常处理某种已知的特定类型的异常,所以它应该抛出其他未知类型的异常。

Expand Down
44 changes: 22 additions & 22 deletions 9-regular-expressions/12-regexp-backreferences/article.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
# Backreferences in pattern: \N and \k<name>

We can use the contents of capturing groups `pattern:(...)` not only in the result or in the replacement string, but also in the pattern itself.

## Backreference by number: \N

A group can be referenced in the pattern using `pattern:\N`, where `N` is the group number.

To make clear why that's helpful, let's consider a task.

We need to find quoted strings: either single-quoted `subject:'...'` or a double-quoted `subject:"..."` -- both variants should match.

How to find them?

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 in the string `subject:"She's the one!"`:

```js run
let str = `He said: "She's the one!".`;

let regexp = /['"](.*?)['"]/g;

// The result is not what we'd like to have
alert( str.match(regexp) ); // "She'
```

As we can see, the pattern found an opening quote `match:"`, then the text is consumed 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 wrap it into a capturing group and backreference it: `pattern:(['"])(.*?)\1`.

Here's the correct code:

```js run
let str = `He said: "She's the one!".`;
Expand All @@ -39,27 +39,27 @@ let regexp = /(['"])(.*?)\1/g;
alert( str.match(regexp) ); // "She's the one!"
```

Now it works! The regular expression engine finds the first quote `pattern:(['"])` and memorizes its content. That's the first capturing group.

Further in the pattern `pattern:\1` means "find the same text as in the first group", exactly the same quote in our case.

Similar to that, `pattern:\2` would mean the contents of the second group, `pattern:\3` - the 3rd group, and so on.

```smart
If we use `?:` in the group, then we can't reference it. Groups that are excluded from capturing `(?:...)` are not memorized by the engine.
```

```warn header="Don't mess up: in the pattern `pattern:\1`, in the replacement: `pattern:$1`"
In the replacement string we use a dollar sign: `pattern:$1`, while in the pattern - a backslash `pattern:\1`.
```

## Backreference by name: `\k<name>`

If a regexp has many parentheses, it's convenient to give them names.

To reference a named group we can use `pattern:\k<имя>`.

In the example below the group with quotes is named `pattern:?<quote>`, so the backreference is `pattern:\k<quote>`:

```js run
let str = `He said: "She's the one!".`;
Expand Down
Toggle all file notes Toggle all file annotations