Skip to content

Commit 29711e0

Browse files
lihanxiangleviding
authored andcommitted
贪婪量词和惰性量词 (javascript-tutorial#237)
* article.md * task.md * solution.md * task.md * solution.md * task.md * solution.md * 根据校对意见进行修改 * 根据校对意见进行修改 * 译文格式修改
1 parent 0bc85b7 commit 29711e0

File tree

7 files changed

+124
-124
lines changed

7 files changed

+124
-124
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
The result is: `match:123 4`.
2+
结果是:`match:123 4`
33

4-
First the lazy `pattern:\d+?` tries to take as little digits as it can, but it has to reach the space, so it takes `match:123`.
4+
首先,懒惰模式 `pattern:\d+?` 尝试去获取尽可能少的字符,但当它检测到空格,就得出匹配结果 `match:123`
55

6-
Then the second `\d+?` takes only one digit, because that's enough.
6+
然后,第二个 `\d+?` 就只获取一个字符,因为这就已足够了。

5-regular-expressions/08-regexp-greedy-and-lazy/1-lazy-greedy/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# A match for /d+? d+?/
1+
# 对于 /d+? d+?/ 的匹配
22

3-
What's the match here?
3+
以下匹配的结果是什么?
44

55
```js
66
"123 456".match(/\d+? \d+?/g) ); // ?

5-regular-expressions/08-regexp-greedy-and-lazy/3-find-html-comments/solution.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
We need to find the beginning of the comment `match:<!--`, then everything till the end of `match:-->`.
1+
我们需要找到注释的起始位置 `match:<!--`,然后获取字符直到注释的末尾 `match:-->`
22

3-
The first idea could be `pattern:<!--.*?-->` -- the lazy quantifier makes the dot stop right before `match:-->`.
3+
首先想到的是 `pattern:<!--.*?-->` —— 惰性量词使得点(.)停在 `match:-->` 之前。
44

5-
But a dot in Javascript means "any symbol except the newline". So multiline comments won't be found.
5+
但是在 Javascript 中,一个点(.)表示除换行符之外的任意字符。所以这是无法匹配多行注释的。
66

7-
We can use `pattern:[\s\S]` instead of the dot to match "anything":
7+
我们可以用 `pattern:[\s\S]`,而不是用点(.)来匹配“任何东西”:
88

99
```js run
1010
let reg = /<!--[\s\S]*?-->/g;

5-regular-expressions/08-regexp-greedy-and-lazy/3-find-html-comments/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Find HTML comments
1+
# 查找 HTML 注释
22

3-
Find all HTML comments in the text:
3+
找出文本中的所有注释:
44

55
```js
6-
let reg = /your regexp/g;
6+
let reg = /你的正则表达式/g;
77

88
let str = `... <!-- My -- comment
99
test --> .. <!----> ..

5-regular-expressions/08-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
The solution is `pattern:<[^<>]+>`.
2+
答案是 `pattern:<[^<>]+>`
33

44
```js run
55
let reg = /<[^<>]+>/g;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
# Find HTML tags
1+
# 寻找 HTML 标签
22

3-
Create a regular expression to find all (opening and closing) HTML tags with their attributes.
3+
创建一个正则表达式语句来寻找所有具有其属性的(闭合或非闭合)HTML 标签。
44

5-
An example of use:
5+
用例:
66

77
```js run
8-
let reg = /your regexp/g;
8+
let reg = /你的正则表达式/g;
99

1010
let str = '<> <a href="/"> <input type="radio" checked> <b>';
1111

1212
alert( str.match(reg) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
1313
```
1414

15-
Let's assume that may not contain `<` and `>` inside (in quotes too), that simplifies things a bit.
15+
假设不包含 `<` `>`(也包括引号),这将会简单许多。

5-regular-expressions/08-regexp-greedy-and-lazy/article.md

Lines changed: 106 additions & 106 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)