From 54543bbc0a6ae0b655240c836c2f25e4e176e936 Mon Sep 17 00:00:00 2001 From: bemself Date: Sun, 10 May 2020 16:11:30 +0800 Subject: [PATCH 1/6] init --- .../16-regexp-sticky/article.md | 102 +++++++++--------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/9-regular-expressions/16-regexp-sticky/article.md b/9-regular-expressions/16-regexp-sticky/article.md index f3650c9169..fd680e0477 100644 --- a/9-regular-expressions/16-regexp-sticky/article.md +++ b/9-regular-expressions/16-regexp-sticky/article.md @@ -1,127 +1,127 @@ -# Sticky flag "y", searching at position +# 粘性标志 "y",在位置搜索 -The flag `pattern:y` allows to perform the search at the given position in the source string. +`pattern:y` 标志允许在源字符串中的指定位置执行搜索。 -To grasp the use case of `pattern:y` flag, and see how great it is, let's explore a practical use case. +为了掌握 `pattern:y` 标志的用例,看看它有多好,让我们来探讨一个实际的用例。 +d +regexps的常见任务之一是"词法分析":我们得到一个文本,比如说在程序设计语言中,我们得到一个文本,然后分析它的结构元素。 -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. +例如,HTML有标签和属性,JavaScript代码有函数、变量等。 -For instance, HTML has tags and attributes, JavaScript code has functions, variables, and so on. +编写词法分析器是一个特殊的领域,有自己的工具和算法,所以我们就不深究了,但有一个共同的任务:在给定的位置读出一些东西。 -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. +例如,我们有一个代码字符串 `subject:let varName = "value"`,我们需要从其中读取变量名,这个变量名从位置 `4` 开始。 -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`. +我们用regexp `pattern:\w+` 来查找变量名。实际上,JavaScript的变量名需要更复杂的regexp来进行准确的匹配,但在这里并不重要。 -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. +调用 `str.match(/\w+/)` 将只找到该行中的第一个单词。或者是所有带标记 `pattern:g` 的单词。但我们只需要在位置 `4` 位置的一个词。 -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`. +要从给定位置搜索,我们可以使用方法 `regexp.exec(str)`。 -To search from the given position, we can use method `regexp.exec(str)`. +如果 `regexp` 没有标志 `pattern:g` 或 `pattern:y`,那么这个方法就可以寻找字符串 `str` 中的第一个匹配,就像 `str.match(regexp)` 一样。这种简单的无标志的情况我们在这里并不感兴趣。 -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. +如果有标志 `pattern:g`,那么它就会在字符串 `str` 中执行搜索,从存储在 `regexp lastIndex` 属性中的位置开始。如果发现匹配,则将 `regexp.lastIndex` 设置为匹配后的索引。 -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. +当一个regexp被创建时,它的 `lastIndex` 是 `0`。 -When a regexp is created, its `lastIndex` is `0`. +因此,连续调用 `regexp.exec(str)` 会一个接一个地返回匹配。 -So, successive calls to `regexp.exec(str)` return matches one after another. +一个例子(用标志 `pattern:g` )。 -An example (with flag `pattern:g`): - -```js run +```js运行 let str = 'let varName'; -let regexp = /\w+/g; -alert(regexp.lastIndex); // 0 (initially lastIndex=0) +let regexp = ///w+/g; +alert(regexp.lastIndex); /// 0 (最初lastIndex=0) let word1 = regexp.exec(str); -alert(word1[0]); // let (1st word) -alert(regexp.lastIndex); // 3 (position after the match) +alert(word1[0]); /// let (1st word) +alert(regexp.lastIndex); //3 (比赛后的位置) let word2 = regexp.exec(str); -alert(word2[0]); // varName (2nd word) -alert(regexp.lastIndex); // 11 (position after the match) +alert(word2[0]); /// varName (第二个单词) +alert(regexp.lastIndex); // 11 (比赛后的位置) let word3 = regexp.exec(str); -alert(word3); // null (no more matches) -alert(regexp.lastIndex); // 0 (resets at search end) +alert(word3); /// null (没有更多的匹配) +alert(regexp.lastIndex); /// 0 (搜索结束时重置) ``` -Every match is returned as an array with groups and additional properties. +每个匹配都会以数组和附加属性的形式返回。 -We can get all matches in the loop: +我们可以在循环中得到所有的匹配。 ```js run let str = 'let varName'; -let regexp = /\w+/g; +let regexp = //w+/g; -let result; +let result.exec(str); let result; while (result = regexp.exec(str)) { - alert( `Found ${result[0]} at position ${result.index}` ); - // Found let at position 0, then - // Found varName at position 4 + alert( `Found ${result[0]}} at position ${result.index}` ); + // 在位置 0 发现 let, 然后 + // 在位置 4 发现 varName } ``` -Such use of `regexp.exec` is an alternative to method `str.matchAll`. +`regexp.exec` 是 `str.matchAll` 方法的替代方法。 -Unlike other methods, we can set our own `lastIndex`, to start the search from the given position. +与其他方法不同,我们可以设置自己的 "lastIndex",从给定位置开始搜索。 -For instance, let's find a word, starting from position `4`: +例如,让我们从位置 `4` 开始寻找一个单词。 ```js run let str = 'let varName = "value"'; -let regexp = /\w+/g; // without flag "g", property lastIndex is ignored +let regexp = //w+/g; //如果没有标志 "g",属性lastIndex会被忽略 *!* regexp.lastIndex = 4; */!* let word = regexp.exec(str); -alert(word); // varName +alert(word); /// varName ``` -We performed a search of `pattern:\w+`, starting from position `regexp.lastIndex = 4`. +我们从位置 `regexp.lastIndex = 4` 开始搜索 `pattern:w+`。 -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: +请注意:搜索从位置 `lastIndex` 开始,然后再往前走。如果在 `lastIndex` 位置上没有词,但它在后面的某个地方,那么它就会被找到。 ```js run let str = 'let varName = "value"'; -let regexp = /\w+/g; +let regexp = //w+/g; *!* regexp.lastIndex = 3; */!* let word = regexp.exec(str); -alert(word[0]); // varName +alert(word[0]); /// varName alert(word.index); // 4 ``` -...So, with flag `pattern:g` property `lastIndex` sets the starting position for the search. +...所以,用标志 `pattern:g` 属性 `lastIndex` 设置搜索的起始位置。 -**Flag `pattern:y` makes `regexp.exec` to look exactly at position `lastIndex`, not before, not after it.** +**标记 `pattern:y` 使 `regexp.exec` 正好在`lastIndex` 位置,而不是在它之前,而不是在它之后。 -Here's the same search with flag `pattern:y`: +下面是使用标志 `pattern:y` 进行同样的搜索。 -```js run +```js运行 let str = 'let varName = "value"'; -let regexp = /\w+/y; +let regexp = //w+/y; regexp.lastIndex = 3; -alert( regexp.exec(str) ); // null (there's a space at position 3, not a word) +alert( regexp.exec(str) ); /// null (位置3有一个空格,不是单词) regexp.lastIndex = 4; -alert( regexp.exec(str) ); // varName (word at position 4) +alert( regexp.exec(str) ); /// varName (word at position 4) ``` -As we can see, regexp `pattern:/\w+/y` doesn't match at position `3` (unlike the flag `pattern:g`), but matches at position `4`. +我们可以看到,regexp `pattern://w+/y` 在位置 `3` 处不匹配(不像标志 `pattern:g` ),而是在位置 `4` 处匹配。 -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`. +想象一下,我们有一个长的文本,而里面根本没有匹配。那么用标志 `pattern:g` 搜索将一直到文本的最后,这将比用标志 `pattern:y` 搜索要花费更多的时间。 -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. +在像词法分析这样的任务中,通常会有很多搜索在一个确切的位置。使用标志 `pattern:y` 是获得良好性能的关键。 \ No newline at end of file From 9b42ff80ecbcda7822c78cb460f6976c9f2c8c49 Mon Sep 17 00:00:00 2001 From: bemself Date: Sun, 10 May 2020 16:14:14 +0800 Subject: [PATCH 2/6] update --- 9-regular-expressions/16-regexp-sticky/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/16-regexp-sticky/article.md b/9-regular-expressions/16-regexp-sticky/article.md index fd680e0477..6a09430479 100644 --- a/9-regular-expressions/16-regexp-sticky/article.md +++ b/9-regular-expressions/16-regexp-sticky/article.md @@ -4,7 +4,7 @@ `pattern:y` 标志允许在源字符串中的指定位置执行搜索。 为了掌握 `pattern:y` 标志的用例,看看它有多好,让我们来探讨一个实际的用例。 -d + regexps的常见任务之一是"词法分析":我们得到一个文本,比如说在程序设计语言中,我们得到一个文本,然后分析它的结构元素。 例如,HTML有标签和属性,JavaScript代码有函数、变量等。 From c4f474a0b8b89844dfc1a47f95cc38b7c8a3b13b Mon Sep 17 00:00:00 2001 From: bemself Date: Sun, 10 May 2020 16:33:12 +0800 Subject: [PATCH 3/6] update --- .../16-regexp-sticky/article.md | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/9-regular-expressions/16-regexp-sticky/article.md b/9-regular-expressions/16-regexp-sticky/article.md index 6a09430479..d61990769e 100644 --- a/9-regular-expressions/16-regexp-sticky/article.md +++ b/9-regular-expressions/16-regexp-sticky/article.md @@ -5,58 +5,58 @@ 为了掌握 `pattern:y` 标志的用例,看看它有多好,让我们来探讨一个实际的用例。 -regexps的常见任务之一是"词法分析":我们得到一个文本,比如说在程序设计语言中,我们得到一个文本,然后分析它的结构元素。 +regexps 的常见任务之一是"词法分析":比如我们在程序设计语言中得到一个文本,然后分析它的结构元素。 -例如,HTML有标签和属性,JavaScript代码有函数、变量等。 +例如,HTML 有标签和属性,JavaScript 代码有函数、变量等。 编写词法分析器是一个特殊的领域,有自己的工具和算法,所以我们就不深究了,但有一个共同的任务:在给定的位置读出一些东西。 例如,我们有一个代码字符串 `subject:let varName = "value"`,我们需要从其中读取变量名,这个变量名从位置 `4` 开始。 -我们用regexp `pattern:\w+` 来查找变量名。实际上,JavaScript的变量名需要更复杂的regexp来进行准确的匹配,但在这里并不重要。 +我们用 regexp `pattern:\w+` 来查找变量名。实际上,JavaScript 的变量名需要更复杂的 regexp 来进行准确的匹配,但在这里并不重要。 -调用 `str.match(/\w+/)` 将只找到该行中的第一个单词。或者是所有带标记 `pattern:g` 的单词。但我们只需要在位置 `4` 位置的一个词。 +调用 `str.match(/\w+/)` 将只找到该行中的第一个单词。或者是所有带标记 `pattern:g` 的单词。但我们只需要在位置 `4` 的一个词。 要从给定位置搜索,我们可以使用方法 `regexp.exec(str)`。 如果 `regexp` 没有标志 `pattern:g` 或 `pattern:y`,那么这个方法就可以寻找字符串 `str` 中的第一个匹配,就像 `str.match(regexp)` 一样。这种简单的无标志的情况我们在这里并不感兴趣。 -如果有标志 `pattern:g`,那么它就会在字符串 `str` 中执行搜索,从存储在 `regexp lastIndex` 属性中的位置开始。如果发现匹配,则将 `regexp.lastIndex` 设置为匹配后的索引。 +如果有标志 `pattern:g`,那么它就会在字符串 `str` 中执行搜索,从存储在 `regexp.lastIndex` 属性中的位置开始。如果发现匹配,则将 `regexp.lastIndex` 设置为匹配后的索引。 -当一个regexp被创建时,它的 `lastIndex` 是 `0`。 +当一个 regexp 被创建时,它的 `lastIndex` 是 `0`。 因此,连续调用 `regexp.exec(str)` 会一个接一个地返回匹配。 -一个例子(用标志 `pattern:g` )。 +一个例子(用标志 `pattern:g` ): -```js运行 +```js run let str = 'let varName'; -let regexp = ///w+/g; -alert(regexp.lastIndex); /// 0 (最初lastIndex=0) +let regexp = /\w+/g; +alert(regexp.lastIndex); // 0(最初 lastIndex=0) let word1 = regexp.exec(str); -alert(word1[0]); /// let (1st word) -alert(regexp.lastIndex); //3 (比赛后的位置) +alert(word1[0]); // let(第一个单词) +alert(regexp.lastIndex); // 3(匹配后的位置) let word2 = regexp.exec(str); -alert(word2[0]); /// varName (第二个单词) -alert(regexp.lastIndex); // 11 (比赛后的位置) +alert(word2[0]); // varName (第二个单词) +alert(regexp.lastIndex); // 11(匹配后的位置) let word3 = regexp.exec(str); -alert(word3); /// null (没有更多的匹配) -alert(regexp.lastIndex); /// 0 (搜索结束时重置) +alert(word3); // null(没有更多的匹配) +alert(regexp.lastIndex); // 0(搜索结束时重置) ``` -每个匹配都会以数组和附加属性的形式返回。 +每个匹配都会以数组形式返回,包含分组和附加属性。 我们可以在循环中得到所有的匹配。 ```js run let str = 'let varName'; -let regexp = //w+/g; +let regexp = /\w+/g; -let result.exec(str); let result; +let result; while (result = regexp.exec(str)) { alert( `Found ${result[0]}} at position ${result.index}` ); @@ -67,61 +67,61 @@ while (result = regexp.exec(str)) { `regexp.exec` 是 `str.matchAll` 方法的替代方法。 -与其他方法不同,我们可以设置自己的 "lastIndex",从给定位置开始搜索。 +与其他方法不同,我们可以设置自己的 `lastIndex`,从给定位置开始搜索。 例如,让我们从位置 `4` 开始寻找一个单词。 ```js run let str = 'let varName = "value"'; -let regexp = //w+/g; //如果没有标志 "g",属性lastIndex会被忽略 +let regexp = /\w+/g; // 如果没有标志 "g",属性 lastIndex 会被忽略 *!* regexp.lastIndex = 4; */!* let word = regexp.exec(str); -alert(word); /// varName +alert(word); // varName ``` 我们从位置 `regexp.lastIndex = 4` 开始搜索 `pattern:w+`。 -请注意:搜索从位置 `lastIndex` 开始,然后再往前走。如果在 `lastIndex` 位置上没有词,但它在后面的某个地方,那么它就会被找到。 +请注意:搜索从位置 `lastIndex` 开始,然后再往前走。如果在 `lastIndex` 位置上没有词,但它在后面的某个地方,那么它就会被找到: ```js run let str = 'let varName = "value"'; -let regexp = //w+/g; +let regexp = /\w+/g; *!* regexp.lastIndex = 3; */!* let word = regexp.exec(str); -alert(word[0]); /// varName +alert(word[0]); // varName alert(word.index); // 4 ``` ...所以,用标志 `pattern:g` 属性 `lastIndex` 设置搜索的起始位置。 -**标记 `pattern:y` 使 `regexp.exec` 正好在`lastIndex` 位置,而不是在它之前,而不是在它之后。 +**标记 `pattern:y` 使 `regexp.exec` 正好在 `lastIndex` 位置,而不是在它之前,也不是在它之后。 下面是使用标志 `pattern:y` 进行同样的搜索。 -```js运行 +```js run let str = 'let varName = "value"'; -let regexp = //w+/y; +let regexp = /\w+/y; regexp.lastIndex = 3; -alert( regexp.exec(str) ); /// null (位置3有一个空格,不是单词) +alert( regexp.exec(str) ); // null(位置 3 有一个空格,不是单词) regexp.lastIndex = 4; -alert( regexp.exec(str) ); /// varName (word at position 4) +alert( regexp.exec(str) ); // varName(在位置 4 的单词) ``` -我们可以看到,regexp `pattern://w+/y` 在位置 `3` 处不匹配(不像标志 `pattern:g` ),而是在位置 `4` 处匹配。 +我们可以看到,regexp `pattern:/\w+/y` 在位置 `3` 处不匹配(不像标志 `pattern:g` ),而是在位置 `4` 处匹配。 想象一下,我们有一个长的文本,而里面根本没有匹配。那么用标志 `pattern:g` 搜索将一直到文本的最后,这将比用标志 `pattern:y` 搜索要花费更多的时间。 -在像词法分析这样的任务中,通常会有很多搜索在一个确切的位置。使用标志 `pattern:y` 是获得良好性能的关键。 \ No newline at end of file +在像词法分析这样的任务中,通常在一个确切的位置会有很多搜索。使用标志 `pattern:y` 是获得良好性能的关键。 \ No newline at end of file From d59509351189c7053696f47d4bb394564a367840 Mon Sep 17 00:00:00 2001 From: bemself Date: Sun, 10 May 2020 16:34:23 +0800 Subject: [PATCH 4/6] update --- 9-regular-expressions/16-regexp-sticky/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/16-regexp-sticky/article.md b/9-regular-expressions/16-regexp-sticky/article.md index d61990769e..a0f77df37d 100644 --- a/9-regular-expressions/16-regexp-sticky/article.md +++ b/9-regular-expressions/16-regexp-sticky/article.md @@ -1,5 +1,5 @@ -# 粘性标志 "y",在位置搜索 +# 粘性标志 "y",在位置处搜索 `pattern:y` 标志允许在源字符串中的指定位置执行搜索。 From f2db1ddafb398d0e072e0c87c900ae939a38b2dd Mon Sep 17 00:00:00 2001 From: bemself <45781328+bemself@users.noreply.github.com> Date: Sun, 10 May 2020 22:41:42 +0800 Subject: [PATCH 5/6] Update 9-regular-expressions/16-regexp-sticky/article.md Co-authored-by: Martin --- 9-regular-expressions/16-regexp-sticky/article.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/9-regular-expressions/16-regexp-sticky/article.md b/9-regular-expressions/16-regexp-sticky/article.md index a0f77df37d..0bf3ba5e77 100644 --- a/9-regular-expressions/16-regexp-sticky/article.md +++ b/9-regular-expressions/16-regexp-sticky/article.md @@ -102,7 +102,7 @@ alert(word[0]); // varName alert(word.index); // 4 ``` -...所以,用标志 `pattern:g` 属性 `lastIndex` 设置搜索的起始位置。 +……所以,用标志 `pattern:g` 属性 `lastIndex` 设置搜索的起始位置。 **标记 `pattern:y` 使 `regexp.exec` 正好在 `lastIndex` 位置,而不是在它之前,也不是在它之后。 @@ -124,4 +124,4 @@ alert( regexp.exec(str) ); // varName(在位置 4 的单词) 想象一下,我们有一个长的文本,而里面根本没有匹配。那么用标志 `pattern:g` 搜索将一直到文本的最后,这将比用标志 `pattern:y` 搜索要花费更多的时间。 -在像词法分析这样的任务中,通常在一个确切的位置会有很多搜索。使用标志 `pattern:y` 是获得良好性能的关键。 \ No newline at end of file +在像词法分析这样的任务中,通常在一个确切的位置会有很多搜索。使用标志 `pattern:y` 是获得良好性能的关键。 From 727e9680bdc03a43322bc5e9619bbca6d0111ac3 Mon Sep 17 00:00:00 2001 From: bemself <45781328+bemself@users.noreply.github.com> Date: Sun, 10 May 2020 22:42:08 +0800 Subject: [PATCH 6/6] Update 9-regular-expressions/16-regexp-sticky/article.md Co-authored-by: Martin --- 9-regular-expressions/16-regexp-sticky/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/16-regexp-sticky/article.md b/9-regular-expressions/16-regexp-sticky/article.md index 0bf3ba5e77..a876bf519a 100644 --- a/9-regular-expressions/16-regexp-sticky/article.md +++ b/9-regular-expressions/16-regexp-sticky/article.md @@ -120,7 +120,7 @@ regexp.lastIndex = 4; alert( regexp.exec(str) ); // varName(在位置 4 的单词) ``` -我们可以看到,regexp `pattern:/\w+/y` 在位置 `3` 处不匹配(不像标志 `pattern:g` ),而是在位置 `4` 处匹配。 +我们可以看到,regexp `pattern:/\w+/y` 在位置 `3` 处不匹配(不同于标志 `pattern:g` ),而是在位置 `4` 处匹配。 想象一下,我们有一个长的文本,而里面根本没有匹配。那么用标志 `pattern:g` 搜索将一直到文本的最后,这将比用标志 `pattern:y` 搜索要花费更多的时间。