From 6593295b6e975d1c93f13f676cde5729b5acbe81 Mon Sep 17 00:00:00 2001 From: wangqi Date: Tue, 24 Mar 2020 10:19:37 +0800 Subject: [PATCH 1/5] feat: add translation of 02-regexp-character-classes --- .../02-regexp-character-classes/article.md | 128 +++++++++--------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/9-regular-expressions/02-regexp-character-classes/article.md b/9-regular-expressions/02-regexp-character-classes/article.md index dbf9328753..94dd363081 100644 --- a/9-regular-expressions/02-regexp-character-classes/article.md +++ b/9-regular-expressions/02-regexp-character-classes/article.md @@ -1,14 +1,14 @@ -# Character classes +# 字符类 -Consider a practical task -- we have a phone number like `"+7(903)-123-45-67"`, and we need to turn it into pure numbers: `79035419441`. +考虑一个实际的任务 -- 我们有一个电话号码,例如 `"+7(903)-123-45-67"`,我们需要将其转换为纯数字: `79035419441` 。 -To do so, we can find and remove anything that's not a number. Character classes can help with that. +为此,我们可以查找并删除所有非数字的内容。字符类可以帮助解决这个问题。 -A *character class* is a special notation that matches any symbol from a certain set. +*字符类*(Character classes)是一个特殊的符号,匹配特定集中的任何符号。 -For the start, let's explore the "digit" class. It's written as `pattern:\d` and corresponds to "any single digit". +首先,让我们探索“数字”类。它写为 `pattern:\d` ,对应于“任何一个数字”。 -For instance, the let's find the first digit in the phone number: +例如,让我们找到电话号码的第一个数字: ```js run let str = "+7(903)-123-45-67"; @@ -18,9 +18,9 @@ let regexp = /\d/; alert( str.match(regexp) ); // 7 ``` -Without the flag `pattern:g`, the regular expression only looks for the first match, that is the first digit `pattern:\d`. +如果没有标志 `pattern:g` ,则正则表达式仅查找第一个匹配项,即第一个数字 `pattern:\d` 。 -Let's add the `pattern:g` flag to find all digits: +让我们添加 `pattern:g` 标志来查找所有数字: ```js run let str = "+7(903)-123-45-67"; @@ -33,24 +33,24 @@ alert( str.match(regexp) ); // array of matches: 7,9,0,3,1,2,3,4,5,6,7 alert( str.match(regexp).join('') ); // 79035419441 ``` -That was a character class for digits. There are other character classes as well. +这是数字的字符类。还有其他字符类。 -Most used are: +最常用的是: -`pattern:\d` ("d" is from "digit") -: A digit: a character from `0` to `9`. +`pattern:\d`("d" 来自 "digit") +:数字:从0到9的字符。 -`pattern:\s` ("s" is from "space") -: A space symbol: includes spaces, tabs `\t`, newlines `\n` and few other rare characters, such as `\v`, `\f` and `\r`. +`pattern:\s`("s" 来自 "space") +:空格符号:包括空格,制表符 `\t` ,换行符 `\n` 和其他少数稀有字符,例如 `\v` ,`\f` 和 `\r` 。 -`pattern:\w` ("w" is from "word") -: A "wordly" character: either a letter of Latin alphabet or a digit or an underscore `_`. Non-Latin letters (like cyrillic or hindi) do not belong to `pattern:\w`. +`pattern:\w`("w" 来自 "word") +:“单词”字符:拉丁字母或数字或下划线 `_` 。非拉丁字母(如西里尔字母或印地文)不属于 `pattern:\w` 。 -For instance, `pattern:\d\s\w` means a "digit" followed by a "space character" followed by a "wordly character", such as `match:1 a`. +例如,`pattern:\d\s\w` 表示“数字”,后跟“空格字符”,后跟“文字字符”,例如 `match:1 a` 。 -**A regexp may contain both regular symbols and character classes.** +**正则表达式可能同时包含常规符号和字符类。** -For instance, `pattern:CSS\d` matches a string `match:CSS` with a digit after it: +例如,`pattern:CSS\d` 匹配字符串 `match:CSS` 与后面的数字: ```js run let str = "Is there CSS4?"; @@ -59,32 +59,32 @@ let regexp = /CSS\d/ alert( str.match(regexp) ); // CSS4 ``` -Also we can use many character classes: +我们还可以使用许多字符类: ```js run alert( "I love HTML5!".match(/\s\w\w\w\w\d/) ); // ' HTML5' ``` -The match (each regexp character class has the corresponding result character): +匹配项(每个正则表达式字符类都有对应的结果字符): ![](love-html5-classes.svg) -## Inverse classes +## 反向类 -For every character class there exists an "inverse class", denoted with the same letter, but uppercased. +对于每个字符类,都有一个“反向类”,用相同的字母表示,但大写。 -The "inverse" means that it matches all other characters, for instance: +“反向”表示它与所有其他字符匹配,例如: `pattern:\D` -: Non-digit: any character except `pattern:\d`, for instance a letter. +: 非数字:除 `pattern:\d` 以外的任何字符,例如字母。 `pattern:\S` -: Non-space: any character except `pattern:\s`, for instance a letter. +: 非空格符号:除 `pattern:\s` 以外的任何字符,例如字母。 `pattern:\W` -: Non-wordly character: anything but `pattern:\w`, e.g a non-latin letter or a space. +: 非单词字符:除 `pattern:\w` 以外的任何字符,例如非拉丁字母或空格。 -In the beginning of the chapter we saw how to make a number-only phone number from a string like `subject:+7(903)-123-45-67`: find all digits and join them. +在这一章的开头,我们看到了如何从 `subject:+7(903)-123-45-67` 这样的字符串中创建一个只包含数字的电话号码: 找到所有的数字并将它们连接起来。 ```js run let str = "+7(903)-123-45-67"; @@ -92,7 +92,7 @@ let str = "+7(903)-123-45-67"; alert( str.match(/\d/g).join('') ); // 79031234567 ``` -An alternative, shorter way is to find non-digits `pattern:\D` and remove them from the string: +另一种快捷的替代方法是查找非数字 `pattern:\D` 并将其从字符串中删除: ```js run let str = "+7(903)-123-45-67"; @@ -100,17 +100,17 @@ let str = "+7(903)-123-45-67"; alert( str.replace(/\D/g, "") ); // 79031234567 ``` -## A dot is "any character" +## 点是“任何字符” -A dot `pattern:.` is a special character class that matches "any character except a newline". +点 `pattern:.` 是一种特殊字符类,它与 “除换行符之外的任何字符” 匹配。 -For instance: +例如: ```js run alert( "Z".match(/./) ); // Z ``` -Or in the middle of a regexp: +或在正则表达式中间: ```js run let regexp = /CS.4/; @@ -120,56 +120,56 @@ alert( "CS-4".match(regexp) ); // CS-4 alert( "CS 4".match(regexp) ); // CS 4 (space is also a character) ``` -Please note that a dot means "any character", but not the "absense of a character". There must be a character to match it: +请注意,点表示“任何字符”,而不是“缺少字符”。必须有一个与之匹配的字符: ```js run alert( "CS4".match(/CS.4/) ); // null, no match because there's no character for the dot ``` -### Dot as literally any character with "s" flag +### 点字面意义上是带有"s"标志的任何字符 -By default, a dot doesn't match the newline character `\n`. +默认情况下,点与换行符 `\n` 不匹配。 -For instance, the regexp `pattern:A.B` matches `match:A`, and then `match:B` with any character between them, except a newline `\n`: +例如,正则表达式 `pattern:A.B` 匹配 `match:A` ,然后匹配 `match:B` 和它们之间的任何字符,除了换行符`\n`: ```js run alert( "A\nB".match(/A.B/) ); // null (no match) ``` -There are many situations when we'd like a dot to mean literally "any character", newline included. +在许多情况下,当我们希望用点来表示“任何字符”(包括换行符)时。 -That's what flag `pattern:s` does. If a regexp has it, then a dot `pattern:.` matches literally any character: +这就是标志 `pattern:s` 所做的。如果有一个正则表达式,则点 `pattern:.` 实际上匹配任何字符: ```js run alert( "A\nB".match(/A.B/s) ); // A\nB (match!) ``` -````warn header="Not supported in Firefox, IE, Edge" -Check for the most recent state of support. At the time of writing it doesn't include Firefox, IE, Edge. +````warn header="不支持Firefox, IE, Edge" +检查 以获得最新的支持状态。在撰写本文时,它不包括Firefox,IE,Edge。 -Luckily, there's an alternative, that works everywhere. We can use a regexp like `pattern:[\s\S]` to match "any character". +幸运的是,有一种替代方法可以在任何地方使用。我们可以使用诸如 `pattern:[\s\S]` 之类的正则表达式来匹配“任何字符”。 ```js run alert( "A\nB".match(/A[\s\S]B/) ); // A\nB (match!) ``` -The pattern `pattern:[\s\S]` literally says: "a space character OR not a space character". In other words, "anything". We could use another pair of complementary classes, such as `pattern:[\d\D]`, that doesn't matter. Or even the `pattern:[^]` -- as it means match any character except nothing. +模式 `pattern:[\s\S]` 从字面上说:“空格字符或非空格字符”。换句话说,“任何东西”。我们可以使用另一对互补的类,例如 `pattern:[\d\D]` 。甚至是 `pattern:[^]` -- 意思是匹配任何字符,除了什么都没有。 -Also we can use this trick if we want both kind of "dots" in the same pattern: the actual dot `pattern:.` behaving the regular way ("not including a newline"), and also a way to match "any character" with `pattern:[\s\S]` or alike. +如果我们希望两种“点”都使用相同的模式,也可以使用此技巧:实际的点 `pattern:.` 具有常规方式(“不包括换行符”)以及一种使用 `pattern:[\s\S]` 或类似形式匹配“任何字符”。 ```` -````warn header="Pay attention to spaces" -Usually we pay little attention to spaces. For us strings `subject:1-5` and `subject:1 - 5` are nearly identical. +````warn header="注意空格" +通常我们很少注意空格。对我们来说,字符串 `subject:1-5` 和 `subject:1 - 5` 几乎相同。 -But if a regexp doesn't take spaces into account, it may fail to work. +但是,如果正则表达式未考虑空格,则可能无法正常工作。 -Let's try to find digits separated by a hyphen: +让我们尝试查找由连字符分隔的数字: ```js run alert( "1 - 5".match(/\d-\d/) ); // null, no match! ``` -Let's fix it adding spaces into the regexp `pattern:\d - \d`: +让我们修复它,在regexp模式中添加空格:\ d-\ d`: ```js run alert( "1 - 5".match(/\d - \d/) ); // 1 - 5, now it works @@ -177,27 +177,27 @@ alert( "1 - 5".match(/\d - \d/) ); // 1 - 5, now it works alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, also works ``` -**A space is a character. Equal in importance with any other character.** +**空格是一个字符。与其他字符同等重要。** -We can't add or remove spaces from a regular expression and expect to work the same. +我们无法在正则表达式中添加或删除空格,并且期望能正常工作。 -In other words, in a regular expression all characters matter, spaces too. +换句话说,在正则表达式中,所有字符都很重要,空格也很重要。 ```` -## Summary +## 总结 -There exist following character classes: +存在以下字符类: -- `pattern:\d` -- digits. -- `pattern:\D` -- non-digits. -- `pattern:\s` -- space symbols, tabs, newlines. -- `pattern:\S` -- all but `pattern:\s`. -- `pattern:\w` -- Latin letters, digits, underscore `'_'`. -- `pattern:\W` -- all but `pattern:\w`. -- `pattern:.` -- any character if with the regexp `'s'` flag, otherwise any except a newline `\n`. +- `pattern:\d` -- 数字。 +- `pattern:\D` -- 非数字。 +- `pattern:\s` -- 空格符号,制表符,换行符。 +- `pattern:\S` -- 除了 `pattern:\s` . +- `pattern:\w` -- 拉丁字母,数字,下划线 `'_'` . +- `pattern:\W` -- 除了 `pattern:\w` . +- `pattern:.` -- 任何带有 `'s'` 标志的字符, 否则为除换行符 `\n` 之外的任何字符。 -...But that's not all! +...但这还不是全部! -Unicode encoding, used by JavaScript for strings, provides many properties for characters, like: which language the letter belongs to (if it's a letter) it is it a punctuation sign, etc. +JavaScript用于字符串的Unicode编码提供了许多字符属性,例如:这个字母属于哪一种语言(如果它是一个字母)?它是标点符号吗?等等。 -We can search by these properties as well. That requires flag `pattern:u`, covered in the next article. +我们也可以通过这些属性进行搜索。这需要标记 `pattern:u` ,在下一篇文章中介绍。 From a03941e5b19e468a690f5aaba2b8acd09d78e810 Mon Sep 17 00:00:00 2001 From: wangqi Date: Tue, 24 Mar 2020 10:35:47 +0800 Subject: [PATCH 2/5] feat: update translation of 02-regexp-character-classes --- .../02-regexp-character-classes/article.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/9-regular-expressions/02-regexp-character-classes/article.md b/9-regular-expressions/02-regexp-character-classes/article.md index 94dd363081..3c6db3fbf4 100644 --- a/9-regular-expressions/02-regexp-character-classes/article.md +++ b/9-regular-expressions/02-regexp-character-classes/article.md @@ -44,9 +44,9 @@ alert( str.match(regexp).join('') ); // 79035419441 :空格符号:包括空格,制表符 `\t` ,换行符 `\n` 和其他少数稀有字符,例如 `\v` ,`\f` 和 `\r` 。 `pattern:\w`("w" 来自 "word") -:“单词”字符:拉丁字母或数字或下划线 `_` 。非拉丁字母(如西里尔字母或印地文)不属于 `pattern:\w` 。 +:“单字”字符:拉丁字母或数字或下划线 `_` 。非拉丁字母(如西里尔字母或印地文)不属于 `pattern:\w` 。 -例如,`pattern:\d\s\w` 表示“数字”,后跟“空格字符”,后跟“文字字符”,例如 `match:1 a` 。 +例如,`pattern:\d\s\w` 表示“数字”,后跟“空格字符”,后跟“单字字符”,例如 `match:1 a` 。 **正则表达式可能同时包含常规符号和字符类。** @@ -82,7 +82,7 @@ alert( "I love HTML5!".match(/\s\w\w\w\w\d/) ); // ' HTML5' : 非空格符号:除 `pattern:\s` 以外的任何字符,例如字母。 `pattern:\W` -: 非单词字符:除 `pattern:\w` 以外的任何字符,例如非拉丁字母或空格。 +: 非单字字符:除 `pattern:\w` 以外的任何字符,例如非拉丁字母或空格。 在这一章的开头,我们看到了如何从 `subject:+7(903)-123-45-67` 这样的字符串中创建一个只包含数字的电话号码: 找到所有的数字并将它们连接起来。 From daa03a3f1ffebd96f7ce9775c2c31f9725fcc0e4 Mon Sep 17 00:00:00 2001 From: wangqi Date: Tue, 24 Mar 2020 12:14:26 +0800 Subject: [PATCH 3/5] feat: Update translation of 9-regular-expressions/02-regexp-character-classes --- .../02-regexp-character-classes/article.md | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/9-regular-expressions/02-regexp-character-classes/article.md b/9-regular-expressions/02-regexp-character-classes/article.md index 3c6db3fbf4..4ba955b37a 100644 --- a/9-regular-expressions/02-regexp-character-classes/article.md +++ b/9-regular-expressions/02-regexp-character-classes/article.md @@ -1,12 +1,12 @@ # 字符类 -考虑一个实际的任务 -- 我们有一个电话号码,例如 `"+7(903)-123-45-67"`,我们需要将其转换为纯数字: `79035419441` 。 +考虑一个实际的任务 - 我们有一个电话号码,例如 `"+7(903)-123-45-67"`,我们需要将其转换为纯数字:`79035419441`。 为此,我们可以查找并删除所有非数字的内容。字符类可以帮助解决这个问题。 -*字符类*(Character classes)是一个特殊的符号,匹配特定集中的任何符号。 +**字符类(Character classes)** 是一个特殊的符号,匹配特定集中的任何符号。 -首先,让我们探索“数字”类。它写为 `pattern:\d` ,对应于“任何一个数字”。 +首先,让我们探索“数字”类。它写为 `pattern:\d`,对应于“任何一个数字”。 例如,让我们找到电话号码的第一个数字: @@ -18,9 +18,9 @@ let regexp = /\d/; alert( str.match(regexp) ); // 7 ``` -如果没有标志 `pattern:g` ,则正则表达式仅查找第一个匹配项,即第一个数字 `pattern:\d` 。 +如果没有标志 `pattern:g`,则正则表达式仅查找第一个匹配项,即第一个数字 `pattern:\d`。 -让我们添加 `pattern:g` 标志来查找所有数字: +让我们添加 `pattern:g`标志来查找所有数字: ```js run let str = "+7(903)-123-45-67"; @@ -38,15 +38,15 @@ alert( str.match(regexp).join('') ); // 79035419441 最常用的是: `pattern:\d`("d" 来自 "digit") -:数字:从0到9的字符。 +: 数字:从0到9的字符。 `pattern:\s`("s" 来自 "space") -:空格符号:包括空格,制表符 `\t` ,换行符 `\n` 和其他少数稀有字符,例如 `\v` ,`\f` 和 `\r` 。 +: 空格符号:包括空格,制表符 `\t`,换行符 `\n` 和其他少数稀有字符,例如 `\v`,`\f` 和 `\r`。 `pattern:\w`("w" 来自 "word") -:“单字”字符:拉丁字母或数字或下划线 `_` 。非拉丁字母(如西里尔字母或印地文)不属于 `pattern:\w` 。 +: “单字”字符:拉丁字母或数字或下划线 `_`。非拉丁字母(如西里尔字母或印地文)不属于 `pattern:\w`。 -例如,`pattern:\d\s\w` 表示“数字”,后跟“空格字符”,后跟“单字字符”,例如 `match:1 a` 。 +例如,`pattern:\d\s\w`表示“数字”,后跟“空格字符”,后跟“单字字符”,例如 `match:1 a`。 **正则表达式可能同时包含常规符号和字符类。** @@ -130,7 +130,7 @@ alert( "CS4".match(/CS.4/) ); // null, no match because there's no character for 默认情况下,点与换行符 `\n` 不匹配。 -例如,正则表达式 `pattern:A.B` 匹配 `match:A` ,然后匹配 `match:B` 和它们之间的任何字符,除了换行符`\n`: +例如,正则表达式 `pattern:A.B` 匹配 `match:A`,然后匹配 `match:B` 和它们之间的任何字符,除了换行符`\n`: ```js run alert( "A\nB".match(/A.B/) ); // null (no match) @@ -153,7 +153,7 @@ alert( "A\nB".match(/A.B/s) ); // A\nB (match!) alert( "A\nB".match(/A[\s\S]B/) ); // A\nB (match!) ``` -模式 `pattern:[\s\S]` 从字面上说:“空格字符或非空格字符”。换句话说,“任何东西”。我们可以使用另一对互补的类,例如 `pattern:[\d\D]` 。甚至是 `pattern:[^]` -- 意思是匹配任何字符,除了什么都没有。 +模式 `pattern:[\s\S]` 从字面上说:“空格字符或非空格字符”。换句话说,“任何东西”。我们可以使用另一对互补的类,例如 `pattern:[\d\D]`。甚至是 `pattern:[^]` - 意思是匹配任何字符,除了什么都没有。 如果我们希望两种“点”都使用相同的模式,也可以使用此技巧:实际的点 `pattern:.` 具有常规方式(“不包括换行符”)以及一种使用 `pattern:[\s\S]` 或类似形式匹配“任何字符”。 ```` @@ -188,16 +188,16 @@ alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, also works 存在以下字符类: -- `pattern:\d` -- 数字。 -- `pattern:\D` -- 非数字。 -- `pattern:\s` -- 空格符号,制表符,换行符。 -- `pattern:\S` -- 除了 `pattern:\s` . -- `pattern:\w` -- 拉丁字母,数字,下划线 `'_'` . -- `pattern:\W` -- 除了 `pattern:\w` . -- `pattern:.` -- 任何带有 `'s'` 标志的字符, 否则为除换行符 `\n` 之外的任何字符。 +- `pattern:\d` - 数字。 +- `pattern:\D` - 非数字。 +- `pattern:\s` - 空格符号,制表符,换行符。 +- `pattern:\S` - 除了 `pattern:\s` 。 +- `pattern:\w` - 拉丁字母,数字,下划线 `'_'`。 +- `pattern:\W` - 除了 `pattern:\w`。 +- `pattern:.` - 任何带有 `'s'` 标志的字符,否则为除换行符 `\n`之外的任何字符。 -...但这还不是全部! +……但这还不是全部! -JavaScript用于字符串的Unicode编码提供了许多字符属性,例如:这个字母属于哪一种语言(如果它是一个字母)?它是标点符号吗?等等。 +JavaScript 用于字符串的 Unicode 编码提供了许多字符属性,例如:这个字母属于哪一种语言(如果它是一个字母)?它是标点符号吗?等等。 -我们也可以通过这些属性进行搜索。这需要标记 `pattern:u` ,在下一篇文章中介绍。 +我们也可以通过这些属性进行搜索。这需要标记 `pattern:u`,在下一篇文章中介绍。 From 3aed3cc399077d32792ee6d407a8e148b6053c19 Mon Sep 17 00:00:00 2001 From: wangqi Date: Tue, 7 Apr 2020 14:09:48 +0800 Subject: [PATCH 4/5] feat: --- .../02-regexp-character-classes/article.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/9-regular-expressions/02-regexp-character-classes/article.md b/9-regular-expressions/02-regexp-character-classes/article.md index 4ba955b37a..40b56d9c5c 100644 --- a/9-regular-expressions/02-regexp-character-classes/article.md +++ b/9-regular-expressions/02-regexp-character-classes/article.md @@ -38,7 +38,7 @@ alert( str.match(regexp).join('') ); // 79035419441 最常用的是: `pattern:\d`("d" 来自 "digit") -: 数字:从0到9的字符。 +: 数字:从 `0` 到 `9` 的字符。 `pattern:\s`("s" 来自 "space") : 空格符号:包括空格,制表符 `\t`,换行符 `\n` 和其他少数稀有字符,例如 `\v`,`\f` 和 `\r`。 @@ -71,7 +71,7 @@ alert( "I love HTML5!".match(/\s\w\w\w\w\d/) ); // ' HTML5' ## 反向类 -对于每个字符类,都有一个“反向类”,用相同的字母表示,但大写。 +对于每个字符类,都有一个“反向类”,用相同的字母表示,但要以大写书写形式。 “反向”表示它与所有其他字符匹配,例如: @@ -100,7 +100,7 @@ let str = "+7(903)-123-45-67"; alert( str.replace(/\D/g, "") ); // 79031234567 ``` -## 点是“任何字符” +## 点(.)是匹配“任何字符” 点 `pattern:.` 是一种特殊字符类,它与 “除换行符之外的任何字符” 匹配。 @@ -126,7 +126,7 @@ alert( "CS 4".match(regexp) ); // CS 4 (space is also a character) alert( "CS4".match(/CS.4/) ); // null, no match because there's no character for the dot ``` -### 点字面意义上是带有"s"标志的任何字符 +### 带有“s”标志时点字符类严格匹配任何字符 默认情况下,点与换行符 `\n` 不匹配。 @@ -144,8 +144,8 @@ alert( "A\nB".match(/A.B/) ); // null (no match) alert( "A\nB".match(/A.B/s) ); // A\nB (match!) ``` -````warn header="不支持Firefox, IE, Edge" -检查 以获得最新的支持状态。在撰写本文时,它不包括Firefox,IE,Edge。 +````warn header="不支持 Firefox、IE、Edge" +使用前可从 确认以获得最新的支持状态。在撰写本文时,它不包括 Firefox、IE、Edge。 幸运的是,有一种替代方法可以在任何地方使用。我们可以使用诸如 `pattern:[\s\S]` 之类的正则表达式来匹配“任何字符”。 @@ -163,13 +163,13 @@ alert( "A\nB".match(/A[\s\S]B/) ); // A\nB (match!) 但是,如果正则表达式未考虑空格,则可能无法正常工作。 -让我们尝试查找由连字符分隔的数字: +让我们尝试查找由连字符(-)分隔的数字: ```js run alert( "1 - 5".match(/\d-\d/) ); // null, no match! ``` -让我们修复它,在regexp模式中添加空格:\ d-\ d`: +让我们修复一下,在正则表达式中添加空格:\ d-\ d`: ```js run alert( "1 - 5".match(/\d - \d/) ); // 1 - 5, now it works @@ -200,4 +200,4 @@ alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, also works JavaScript 用于字符串的 Unicode 编码提供了许多字符属性,例如:这个字母属于哪一种语言(如果它是一个字母)?它是标点符号吗?等等。 -我们也可以通过这些属性进行搜索。这需要标记 `pattern:u`,在下一篇文章中介绍。 +我们也可以通过这些属性进行搜索。这需要标志 `pattern:u`,在下一篇文章中介绍。 From b1cc03c9ac33ca34bcdcfecf6bb077ac8abee4da Mon Sep 17 00:00:00 2001 From: LeviDing Date: Sat, 11 Apr 2020 09:10:23 +0800 Subject: [PATCH 5/5] Update article.md --- .../02-regexp-character-classes/article.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/9-regular-expressions/02-regexp-character-classes/article.md b/9-regular-expressions/02-regexp-character-classes/article.md index 40b56d9c5c..24504ae9a6 100644 --- a/9-regular-expressions/02-regexp-character-classes/article.md +++ b/9-regular-expressions/02-regexp-character-classes/article.md @@ -153,7 +153,7 @@ alert( "A\nB".match(/A.B/s) ); // A\nB (match!) alert( "A\nB".match(/A[\s\S]B/) ); // A\nB (match!) ``` -模式 `pattern:[\s\S]` 从字面上说:“空格字符或非空格字符”。换句话说,“任何东西”。我们可以使用另一对互补的类,例如 `pattern:[\d\D]`。甚至是 `pattern:[^]` - 意思是匹配任何字符,除了什么都没有。 +模式 `pattern:[\s\S]` 从字面上说:“空格字符或非空格字符”。换句话说,“任何东西”。我们可以使用另一对互补的类,例如 `pattern:[\d\D]`。甚至是 `pattern:[^]` —— 意思是匹配任何字符,除了什么都没有。 如果我们希望两种“点”都使用相同的模式,也可以使用此技巧:实际的点 `pattern:.` 具有常规方式(“不包括换行符”)以及一种使用 `pattern:[\s\S]` 或类似形式匹配“任何字符”。 ```` @@ -188,13 +188,13 @@ alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, also works 存在以下字符类: -- `pattern:\d` - 数字。 -- `pattern:\D` - 非数字。 -- `pattern:\s` - 空格符号,制表符,换行符。 -- `pattern:\S` - 除了 `pattern:\s` 。 -- `pattern:\w` - 拉丁字母,数字,下划线 `'_'`。 -- `pattern:\W` - 除了 `pattern:\w`。 -- `pattern:.` - 任何带有 `'s'` 标志的字符,否则为除换行符 `\n`之外的任何字符。 +- `pattern:\d` —— 数字。 +- `pattern:\D` —— 非数字。 +- `pattern:\s` —— 空格符号,制表符,换行符。 +- `pattern:\S` —— 除了 `pattern:\s` 。 +- `pattern:\w` —— 拉丁字母,数字,下划线 `'_'`。 +- `pattern:\W` —— 除了 `pattern:\w`。 +- `pattern:.` —— 任何带有 `'s'` 标志的字符,否则为除换行符 `\n`之外的任何字符。 ……但这还不是全部!