Skip to content

Commit aeaf925

Browse files
authored
Merge pull request #740 from bemself/sticky
9-regular-expressions/16-regexp-sticky/
2 parents 44273ea + 727e968 commit aeaf925

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

Diff for: 9-regular-expressions/16-regexp-sticky/article.md

+41-41
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
11

2-
# Sticky flag "y", searching at position
2+
# 粘性标志 "y",在位置处搜索
33

4-
The flag `pattern:y` allows to perform the search at the given position in the source string.
4+
`pattern:y` 标志允许在源字符串中的指定位置执行搜索。
55

6-
To grasp the use case of `pattern:y` flag, and see how great it is, let's explore a practical use case.
6+
为了掌握 `pattern:y` 标志的用例,看看它有多好,让我们来探讨一个实际的用例。
77

8-
One of common tasks for regexps is "lexical analysis": we get a text, e.g. in a programming language, and analyze it for structural elements.
8+
regexps 的常见任务之一是"词法分析":比如我们在程序设计语言中得到一个文本,然后分析它的结构元素。
99

10-
For instance, HTML has tags and attributes, JavaScript code has functions, variables, and so on.
10+
例如,HTML 有标签和属性,JavaScript 代码有函数、变量等。
1111

12-
Writing lexical analyzers is a special area, with its own tools and algorithms, so we don't go deep in there, but there's a common task: to read something at the given position.
12+
编写词法分析器是一个特殊的领域,有自己的工具和算法,所以我们就不深究了,但有一个共同的任务:在给定的位置读出一些东西。
1313

14-
E.g. we have a code string `subject:let varName = "value"`, and we need to read the variable name from it, that starts at position `4`.
14+
例如,我们有一个代码字符串 `subject:let varName = "value"`,我们需要从其中读取变量名,这个变量名从位置 `4` 开始。
1515

16-
We'll look for variable name using regexp `pattern:\w+`. Actually, JavaScript variable names need a bit more complex regexp for accurate matching, but here it doesn't matter.
16+
我们用 regexp `pattern:\w+` 来查找变量名。实际上,JavaScript 的变量名需要更复杂的 regexp 来进行准确的匹配,但在这里并不重要。
1717

18-
A call to `str.match(/\w+/)` will find only the first word in the line. Or all words with the flag `pattern:g`. But we need only one word at position `4`.
18+
调用 `str.match(/\w+/)` 将只找到该行中的第一个单词。或者是所有带标记 `pattern:g` 的单词。但我们只需要在位置 `4` 的一个词。
1919

20-
To search from the given position, we can use method `regexp.exec(str)`.
20+
要从给定位置搜索,我们可以使用方法 `regexp.exec(str)`
2121

22-
If the `regexp` doesn't have flags `pattern:g` or `pattern:y`, then this method looks for the first match in the string `str`, exactly like `str.match(regexp)`. Such simple no-flags case doesn't interest us here.
22+
如果 `regexp` 没有标志 `pattern:g` `pattern:y`,那么这个方法就可以寻找字符串 `str` 中的第一个匹配,就像 `str.match(regexp)` 一样。这种简单的无标志的情况我们在这里并不感兴趣。
2323

24-
If there's flag `pattern:g`, then it performs the search in the string `str`, starting from position stored in its `regexp.lastIndex` property. And, if it finds a match, then sets `regexp.lastIndex` to the index immediately after the match.
24+
如果有标志 `pattern:g`,那么它就会在字符串 `str` 中执行搜索,从存储在 `regexp.lastIndex` 属性中的位置开始。如果发现匹配,则将 `regexp.lastIndex` 设置为匹配后的索引。
2525

26-
When a regexp is created, its `lastIndex` is `0`.
26+
当一个 regexp 被创建时,它的 `lastIndex` `0`
2727

28-
So, successive calls to `regexp.exec(str)` return matches one after another.
28+
因此,连续调用 `regexp.exec(str)` 会一个接一个地返回匹配。
2929

30-
An example (with flag `pattern:g`):
30+
一个例子(用标志 `pattern:g` ):
3131

3232
```js run
3333
let str = 'let varName';
3434

3535
let regexp = /\w+/g;
36-
alert(regexp.lastIndex); // 0 (initially lastIndex=0)
36+
alert(regexp.lastIndex); // 0(最初 lastIndex=0
3737

3838
let word1 = regexp.exec(str);
39-
alert(word1[0]); // let (1st word)
40-
alert(regexp.lastIndex); // 3 (position after the match)
39+
alert(word1[0]); // let(第一个单词)
40+
alert(regexp.lastIndex); // 3(匹配后的位置)
4141

4242
let word2 = regexp.exec(str);
43-
alert(word2[0]); // varName (2nd word)
44-
alert(regexp.lastIndex); // 11 (position after the match)
43+
alert(word2[0]); // varName (第二个单词)
44+
alert(regexp.lastIndex); // 11(匹配后的位置)
4545

4646
let word3 = regexp.exec(str);
47-
alert(word3); // null (no more matches)
48-
alert(regexp.lastIndex); // 0 (resets at search end)
47+
alert(word3); // null(没有更多的匹配)
48+
alert(regexp.lastIndex); // 0(搜索结束时重置)
4949
```
5050

51-
Every match is returned as an array with groups and additional properties.
51+
每个匹配都会以数组形式返回,包含分组和附加属性。
5252

53-
We can get all matches in the loop:
53+
我们可以在循环中得到所有的匹配。
5454

5555
```js run
5656
let str = 'let varName';
@@ -59,22 +59,22 @@ let regexp = /\w+/g;
5959
let result;
6060

6161
while (result = regexp.exec(str)) {
62-
alert( `Found ${result[0]} at position ${result.index}` );
63-
// Found let at position 0, then
64-
// Found varName at position 4
62+
alert( `Found ${result[0]}} at position ${result.index}` );
63+
// 在位置 0 发现 let, 然后
64+
// 在位置 4 发现 varName
6565
}
6666
```
6767

68-
Such use of `regexp.exec` is an alternative to method `str.matchAll`.
68+
`regexp.exec` `str.matchAll` 方法的替代方法。
6969

70-
Unlike other methods, we can set our own `lastIndex`, to start the search from the given position.
70+
与其他方法不同,我们可以设置自己的 `lastIndex`,从给定位置开始搜索。
7171

72-
For instance, let's find a word, starting from position `4`:
72+
例如,让我们从位置 `4` 开始寻找一个单词。
7373

7474
```js run
7575
let str = 'let varName = "value"';
7676

77-
let regexp = /\w+/g; // without flag "g", property lastIndex is ignored
77+
let regexp = /\w+/g; // 如果没有标志 "g",属性 lastIndex 会被忽略
7878

7979
*!*
8080
regexp.lastIndex = 4;
@@ -84,9 +84,9 @@ let word = regexp.exec(str);
8484
alert(word); // varName
8585
```
8686

87-
We performed a search of `pattern:\w+`, starting from position `regexp.lastIndex = 4`.
87+
我们从位置 `regexp.lastIndex = 4` 开始搜索 `pattern:w+`
8888

89-
Please note: the search starts at position `lastIndex` and then goes further. If there's no word at position `lastIndex`, but it's somewhere after it, then it will be found:
89+
请注意:搜索从位置 `lastIndex` 开始,然后再往前走。如果在 `lastIndex` 位置上没有词,但它在后面的某个地方,那么它就会被找到:
9090

9191
```js run
9292
let str = 'let varName = "value"';
@@ -102,26 +102,26 @@ alert(word[0]); // varName
102102
alert(word.index); // 4
103103
```
104104

105-
...So, with flag `pattern:g` property `lastIndex` sets the starting position for the search.
105+
……所以,用标志 `pattern:g` 属性 `lastIndex` 设置搜索的起始位置。
106106

107-
**Flag `pattern:y` makes `regexp.exec` to look exactly at position `lastIndex`, not before, not after it.**
107+
**标记 `pattern:y` 使 `regexp.exec` 正好在 `lastIndex` 位置,而不是在它之前,也不是在它之后。
108108

109-
Here's the same search with flag `pattern:y`:
109+
下面是使用标志 `pattern:y` 进行同样的搜索。
110110

111111
```js run
112112
let str = 'let varName = "value"';
113113

114114
let regexp = /\w+/y;
115115

116116
regexp.lastIndex = 3;
117-
alert( regexp.exec(str) ); // null (there's a space at position 3, not a word)
117+
alert( regexp.exec(str) ); // null(位置 3 有一个空格,不是单词)
118118

119119
regexp.lastIndex = 4;
120-
alert( regexp.exec(str) ); // varName (word at position 4)
120+
alert( regexp.exec(str) ); // varName(在位置 4 的单词)
121121
```
122122

123-
As we can see, regexp `pattern:/\w+/y` doesn't match at position `3` (unlike the flag `pattern:g`), but matches at position `4`.
123+
我们可以看到,regexp `pattern:/\w+/y` 在位置 `3` 处不匹配(不同于标志 `pattern:g` ),而是在位置 `4` 处匹配。
124124

125-
Imagine, we have a long text, and there are no matches in it, at all. Then searching with flag `pattern:g` will go till the end of the text, and this will take significantly more time than the search with flag `pattern:y`.
125+
想象一下,我们有一个长的文本,而里面根本没有匹配。那么用标志 `pattern:g` 搜索将一直到文本的最后,这将比用标志 `pattern:y` 搜索要花费更多的时间。
126126

127-
In such tasks like lexical analysis, there are usually many searches at an exact position. Using flag `pattern:y` is the key for a good performance.
127+
在像词法分析这样的任务中,通常在一个确切的位置会有很多搜索。使用标志 `pattern:y` 是获得良好性能的关键。

0 commit comments

Comments
 (0)