You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For instance, HTML has tags and attributes, JavaScript code has functions, variables, and so on.
10
+
例如,HTML 有标签和属性,JavaScript 代码有函数、变量等。
11
11
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.
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.
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`.
To search from the given position, we can use method `regexp.exec(str)`.
20
+
要从给定位置搜索,我们可以使用方法 `regexp.exec(str)`。
21
21
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.
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.
alert(regexp.lastIndex); // 3 (position after the match)
39
+
alert(word1[0]); // let(第一个单词)
40
+
alert(regexp.lastIndex); // 3(匹配后的位置)
41
41
42
42
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(匹配后的位置)
45
45
46
46
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(搜索结束时重置)
49
49
```
50
50
51
-
Every match is returned as an array with groups and additional properties.
51
+
每个匹配都会以数组形式返回,包含分组和附加属性。
52
52
53
-
We can get all matches in the loop:
53
+
我们可以在循环中得到所有的匹配。
54
54
55
55
```js run
56
56
let str ='let varName';
@@ -59,22 +59,22 @@ let regexp = /\w+/g;
59
59
let result;
60
60
61
61
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
65
65
}
66
66
```
67
67
68
-
Such use of `regexp.exec`is an alternative to method `str.matchAll`.
68
+
`regexp.exec`是 `str.matchAll` 方法的替代方法。
69
69
70
-
Unlike other methods, we can set our own `lastIndex`, to start the search from the given position.
70
+
与其他方法不同,我们可以设置自己的 `lastIndex`,从给定位置开始搜索。
71
71
72
-
For instance, let's find a word, starting from position `4`:
72
+
例如,让我们从位置 `4` 开始寻找一个单词。
73
73
74
74
```js run
75
75
let str ='let varName = "value"';
76
76
77
-
let regexp =/\w+/g; //without flag "g", property lastIndex is ignored
77
+
let regexp =/\w+/g; //如果没有标志 "g",属性 lastIndex 会被忽略
78
78
79
79
*!*
80
80
regexp.lastIndex=4;
@@ -84,9 +84,9 @@ let word = regexp.exec(str);
84
84
alert(word); // varName
85
85
```
86
86
87
-
We performed a search of`pattern:\w+`, starting from position `regexp.lastIndex = 4`.
87
+
我们从位置 `regexp.lastIndex = 4` 开始搜索`pattern:w+`。
88
88
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:
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`.
0 commit comments