Skip to content

Commit cfbf58a

Browse files
yyxygitbemself
andauthored
Backreferences in pattern: \N and \k<name> (#592)
* 9-regular-expressions/12-regexp-backreferences translation * change request edit * 一点格式的小改动 Co-authored-by: bemself <45781328+bemself@users.noreply.github.com>
1 parent 95a4b67 commit cfbf58a

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

1-js/06-advanced-functions/01-recursion/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ list.next = list.next.next;
504504

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

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

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

1-js/10-error-handling/1-try-catch/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ try {
653653
- `name` —— 异常名称(异常对象的构造函数的名称)。
654654
- `stack`(没有标准) —— 异常发生时的调用栈。
655655

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

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

9-regular-expressions/12-regexp-backreferences/article.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
# Backreferences in pattern: \N and \k<name>
1+
# 模式中的反向引用:\N \k<name>
22

3-
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.
3+
我们不仅可以在结果或替换字符串中使用捕获组 `pattern:(...)` 的内容,还可以在模式本身中使用它们。
44

5-
## Backreference by number: \N
5+
## 按编号反向引用:\N
66

7-
A group can be referenced in the pattern using `pattern:\N`, where `N` is the group number.
7+
可以使用 `pattern:\N` 在模式中引用一个组,其中 `N` 是组号。
88

9-
To make clear why that's helpful, let's consider a task.
9+
为了弄清那为什么有帮助,让我们考虑一项任务。
1010

11-
We need to find quoted strings: either single-quoted `subject:'...'` or a double-quoted `subject:"..."` -- both variants should match.
11+
我们需要找到带引号的字符串:单引号 `subject:'...'` 或双引号 `subject:"..."`– 应匹配两种变体。
1212

13-
How to find them?
13+
如何找到它们?
1414

15-
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!"`:
15+
我们可以将两种引号放在方括号中:`pattern:['"](.*?)['"]`,但它会找到带有混合引号的字符串,例如 `match:"...'` `match:'..."`。当一种引号出现在另一种引号内,比如在字符串 `subject:"She's the one!"` 中时,便会导致不正确的匹配:
1616

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

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

22-
// The result is not what we'd like to have
22+
// 不是我们想要的结果
2323
alert( str.match(regexp) ); // "She'
2424
```
2525

26-
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.
26+
如我们所见,该模式找到了一个开头的引号 `match:"`,然后文本被匹配,直到另一个引号 `match:'`,该匹配结束。
2727

28-
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`.
28+
为了确保模式查找的结束引号与开始的引号完全相同,我们可以将其包装到捕获组中并对其进行反向引用:`pattern:(['"])(.*?)\1`
2929

30-
Here's the correct code:
30+
这是正确的代码:
3131

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

42-
Now it works! The regular expression engine finds the first quote `pattern:(['"])` and memorizes its content. That's the first capturing group.
42+
现在可以了!正则表达式引擎会找到第一个引号 `pattern:(['"])` 并记住其内容。那是第一个捕获组。
4343

44-
Further in the pattern `pattern:\1` means "find the same text as in the first group", exactly the same quote in our case.
44+
`pattern:\1` 在模式中进一步的含义是“查找与第一(捕获)分组相同的文本”,在我们的示例中为完全相同的引号。
4545

46-
Similar to that, `pattern:\2` would mean the contents of the second group, `pattern:\3` - the 3rd group, and so on.
46+
与此类似,`pattern:\2` 表示第二(捕获)分组的内容,`pattern:\3` – 第三分组,依此类推。
4747

4848
```smart
49-
If we use `?:` in the group, then we can't reference it. Groups that are excluded from capturing `(?:...)` are not memorized by the engine.
49+
如果我们在组中使用 `?:`,那么我们将无法引用它。用 `(?:...)` 捕获的组被排除,引擎不会存储。
5050
```
5151

52-
```warn header="Don't mess up: in the pattern `pattern:\1`, in the replacement: `pattern:$1`"
53-
In the replacement string we use a dollar sign: `pattern:$1`, while in the pattern - a backslash `pattern:\1`.
52+
```warn header="不要搞混了: 在模式中用 `pattern:\1`,在替换项中用:`pattern:$1`"
53+
在替换字符串中我们使用美元符号:`pattern:$1`,而在模式中 - 使用反斜杠 `pattern:\1`
5454
```
5555
56-
## Backreference by name: `\k<name>`
56+
## 按命名反向引用:`\k<name>`
5757
58-
If a regexp has many parentheses, it's convenient to give them names.
58+
如果正则表达式中有很多括号对(注:捕获组),给它们起个名字方便引用。
5959
60-
To reference a named group we can use `pattern:\k<имя>`.
60+
要引用命名组,我们可以使用:`pattern:\k<name>`。
6161
62-
In the example below the group with quotes is named `pattern:?<quote>`, so the backreference is `pattern:\k<quote>`:
62+
在下面的示例中引号组命名为 `pattern:?<quote>`,因此反向引用为 `pattern:\k<quote>`
6363
6464
```js run
6565
let str = `He said: "She's the one!".`;

0 commit comments

Comments
 (0)