From 6aa37106ed1441bd6dea65cc46a9a01dcd08d91d Mon Sep 17 00:00:00 2001 From: starshiro Date: Fri, 3 Jan 2020 19:18:21 +0800 Subject: [PATCH 1/6] Added translation for the first section --- .../03-regexp-unicode/article.md | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/9-regular-expressions/03-regexp-unicode/article.md b/9-regular-expressions/03-regexp-unicode/article.md index 82c00d7dfb..075c3709ca 100644 --- a/9-regular-expressions/03-regexp-unicode/article.md +++ b/9-regular-expressions/03-regexp-unicode/article.md @@ -1,24 +1,31 @@ # Unicode: flag "u" and class \p{...} +# Unicode: 修饰符“u”和 class \p{...} JavaScript uses [Unicode encoding](https://en.wikipedia.org/wiki/Unicode) for strings. Most characters are encoding with 2 bytes, but that allows to represent at most 65536 characters. +JavaScript 使用 [Unicode 编码](https://en.wikipedia.org/wiki/Unicode) (Unicode encoding)对字符串进行编码。但这种方式只能编码最多 65536 个字符。 That range is not big enough to encode all possible characters, that's why some rare characters are encoded with 4 bytes, for instance like `𝒳` (mathematical X) or `😄` (a smile), some hieroglyphs and so on. +这个范围不足以对所有可能的字符进行编码,这就是为什么一些罕见的字符使用 4 个字节进行编码,比如 `𝒳` (数学符号 X)或者 `😄` (笑脸),一些象形文字等等。 Here are the unicode values of some characters: +以下是一些字符对应的 unicode 编码: -| Character | Unicode | Bytes count in unicode | -|------------|---------|--------| -| a | `0x0061` | 2 | -| ≈ | `0x2248` | 2 | -|𝒳| `0x1d4b3` | 4 | -|𝒴| `0x1d4b4` | 4 | -|😄| `0x1f604` | 4 | +| 字符 | Unicode | unicode 中的字节数 | +| ------------ | --------- | ---------------------- | +| a | `0x0061` | 2 | +| ≈ | `0x2248` | 2 | +| 𝒳 | `0x1d4b3` | 4 | +| 𝒴 | `0x1d4b4` | 4 | +| 😄 | `0x1f604` | 4 | So characters like `a` and `≈` occupy 2 bytes, while codes for `𝒳`, `𝒴` and `😄` are longer, they have 4 bytes. +所以像 `a` 和 `≈` 这样的字符占用 2 个字节,而 `𝒳`,`𝒴` 和 `😄` 的对应编码则更长,它们具有 4 个字节的长度。 Long time ago, when JavaScript language was created, Unicode encoding was simpler: there were no 4-byte characters. So, some language features still handle them incorrectly. +很久以前,当 JavaScript 被发明出来的时候,Unicode 的编码要更加简单:当时并没有 4 个字节长的字符。所以,一部分语言特性在现在仍旧对无法对 unicode 进行正确的处理。 For instance, `length` thinks that here are two characters: +比如 `length` 认为这里的输入有 2 个字符: ```js run alert('😄'.length); // 2 @@ -26,10 +33,13 @@ alert('𝒳'.length); // 2 ``` ...But we can see that there's only one, right? The point is that `length` treats 4 bytes as two 2-byte characters. That's incorrect, because they must be considered only together (so-called "surrogate pair", you can read about them in the article ). +...但我们可以清楚地认识到输入的字符只有一个,对吧?关键在于 `length` 把 4 个字节当成了 2 个 2 字节长的字符。这是不对的,因为它们必须被当作一个整体来考虑。(即所谓的“代理伪字符”(surrogate pair),你可以在这里进一步阅读有关的的信息 )。 By default, regular expressions also treat 4-byte "long characters" as a pair of 2-byte ones. And, as it happens with strings, that may lead to odd results. We'll see that a bit later, in the article . +默认情况下,正则表达式同样把一个 4 个字节的“长字符”当成一对 2 个字节长的字符。正如在字符串中遇到的情况,这将导致一些奇怪的结果。我们将很快在后面的文章中遇到 . Unlike strings, regular expressions have flag `pattern:u` that fixes such problems. With such flag, a regexp handles 4-byte characters correctly. And also Unicode property search becomes available, we'll get to it next. +与字符串有所不同的是,正则表达式有一个修饰符 `pattern:u` 被用以解决此类问题。当一个正则表达式使用这个修饰符后,4 个字节长的字符将被正确地处理。同时也能够用上 Unicode 属性(Unicode property)来进行查找了。我们接下来就来了解这方面的内容。 ## Unicode properties \p{...} From 53240a17e1dbe74c042c97f5ae8bad031b3074f2 Mon Sep 17 00:00:00 2001 From: starshiro Date: Fri, 3 Jan 2020 20:30:24 +0800 Subject: [PATCH 2/6] Added translation for the second section: unicode property --- .../03-regexp-unicode/article.md | 97 +++++++++++-------- 1 file changed, 58 insertions(+), 39 deletions(-) diff --git a/9-regular-expressions/03-regexp-unicode/article.md b/9-regular-expressions/03-regexp-unicode/article.md index 075c3709ca..adb62dc179 100644 --- a/9-regular-expressions/03-regexp-unicode/article.md +++ b/9-regular-expressions/03-regexp-unicode/article.md @@ -42,84 +42,103 @@ Unlike strings, regular expressions have flag `pattern:u` that fixes such proble 与字符串有所不同的是,正则表达式有一个修饰符 `pattern:u` 被用以解决此类问题。当一个正则表达式使用这个修饰符后,4 个字节长的字符将被正确地处理。同时也能够用上 Unicode 属性(Unicode property)来进行查找了。我们接下来就来了解这方面的内容。 ## Unicode properties \p{...} +## Unicode 属性(Unicode properties)\p{...} ```warn header="Not supported in Firefox and Edge" Despite being a part of the standard since 2018, unicode proeprties are not supported in Firefox ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1361876)) and Edge ([bug](https://github.com/Microsoft/ChakraCore/issues/2969)). +尽管 unicode property 从 2018 年以来便作为标准的一部分, 但 unicode 属性在 Firefox ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1361876)) 和 Edge ([bug](https://github.com/Microsoft/ChakraCore/issues/2969)) 中并没有相应的支持。 There's [XRegExp](http://xregexp.com) library that provides "extended" regular expressions with cross-browser support for unicode properties. +目前 [XRegExp](http://xregexp.com) 这个库提供“扩展”的正则表达式,其中包括对 unicode property 的跨平台支持。 ``` Every character in Unicode has a lot of properties. They describe what "category" the character belongs to, contain miscellaneous information about it. +Unicode 中的每一个字符都具有很多的属性。它们描述了一个字符属于哪个“类别”,包含了各种关于字符的信息。 For instance, if a character has `Letter` property, it means that the character belongs to an alphabet (of any language). And `Number` property means that it's a digit: maybe Arabic or Chinese, and so on. +例如,如果一个字符具有 `Letter` 属性,这意味着这个字符归属于(任意语言的)一个字母表。而 `Number` 属性则表示这是一个数字:也许是阿拉伯语,亦或者是中文,等等。 We can search for characters with a property, written as `pattern:\p{…}`. To use `pattern:\p{…}`, a regular expression must have flag `pattern:u`. +我们可以查找具有某种属性的字符,写作 `pattern:\p{…}`。为了顺利使用 `pattern:\p{…}`,一个正则表达式必须使用修饰符 `pattern:u`。 For instance, `\p{Letter}` denotes a letter in any of language. We can also use `\p{L}`, as `L` is an alias of `Letter`. There are shorter aliases for almost every property. +举个例子,`\p{Letter}` 表示任何语言中的一个字母。我们也可以使用 `\p{L}`,因为 `L` 是 `Letter` 的一个别名(alias)。对于每种属性而言,几乎都存在对应的缩写别名。 In the example below three kinds of letters will be found: English, Georgean and Korean. +在下面的例子中 3 种字母将会被查找出: 英语、格鲁吉亚语和韩语。 ```js run let str = "A ბ ㄱ"; alert( str.match(/\p{L}/gu) ); // A,ბ,ㄱ -alert( str.match(/\p{L}/g) ); // null (no matches, as there's no flag "u") +alert( str.match(/\p{L}/g) ); // null(没有匹配的文本,因为没有修饰符“u”) ``` Here's the main character categories and their subcategories: - -- Letter `L`: - - lowercase `Ll` - - modifier `Lm`, - - titlecase `Lt`, - - uppercase `Lu`, - - other `Lo`. -- Number `N`: - - decimal digit `Nd`, - - letter number `Nl`, - - other `No`. -- Punctuation `P`: - - connector `Pc`, - - dash `Pd`, - - initial quote `Pi`, - - final quote `Pf`, - - open `Ps`, - - close `Pe`, - - other `Po`. -- Mark `M` (accents etc): - - spacing combining `Mc`, - - enclosing `Me`, - - non-spacing `Mn`. -- Symbol `S`: - - currency `Sc`, - - modifier `Sk`, - - math `Sm`, - - other `So`. -- Separator `Z`: - - line `Zl`, - - paragraph `Zp`, - - space `Zs`. -- Other `C`: - - control `Cc`, - - format `Cf`, - - not assigned `Cn`, - -- private use `Co`, - - surrogate `Cs`. +以下是主要的字符类别和它们对应的子类别: + +- 字母(Letter) `L`: + - 小写(lowercase) `Ll` + - 修饰(modifier) `Lm`, + - 标题(titlecase) `Lt`, + - 大写(uppercase) `Lu`, + - 其它(other) `Lo`. +- 数字(Number) `N`: + - 十进制数字(decimal digit) `Nd`, + - 字母数字(letter number) `Nl`, + - 其它(other) `No`. +- 标点符号(Punctuation) `P`: + - 链接符(connector) `Pc`, + - 横杠(dash) `Pd`, + - 起始引用号(initial quote) `Pi`, + - 结束引用号(final quote) `Pf`, + - 开(open) `Ps`, + - 闭(close) `Pe`, + - 其它(other) `Po`. +- 标记(Mark) `M` (accents etc): + - 间隔合并(spacing combining) `Mc`, + - 封闭(enclosing) `Me`, + - 非间隔(non-spacing) `Mn`. +- 符号(Symbol) `S`: + - 货币(currency) `Sc`, + - 修饰(modifier) `Sk`, + - 数学(math) `Sm`, + - 其它(other) `So`. +- 分隔符(Separator) `Z`: + - 行(line) `Zl`, + - 段落(paragraph) `Zp`, + - 空格(space) `Zs`. +- 其它(Other) `C`: + - 控制符(control) `Cc`, + - 格式(format) `Cf`, + - 为分配(not assigned) `Cn`, + - 私有(private use) `Co`, + - 代理伪字符(surrogate) `Cs`. So, e.g. if we need letters in lower case, we can write `pattern:\p{Ll}`, punctuation signs: `pattern:\p{P}` and so on. +所以,比如说我们需要小写的字母,就可以写成 `pattern:\p{Ll}`,标点符号写作 `pattern:\p{P}` 等等。 There are also other derived categories, like: +也有其它派生的类别,例如: - `Alphabetic` (`Alpha`), includes Letters `L`, plus letter numbers `Nl` (e.g. Ⅻ - a character for the roman number 12), plus some other symbols `Other_Alphabetic` (`OAlpha`). +- `Alphabetic` (`Alpha`), 包含了字母 `L`, 加上字母数字 `Nl` (例如 Ⅻ - 罗马数字 12),加上一些其它符号 `Other_Alphabetic` (`OAlpha`). - `Hex_Digit` includes hexadecimal digits: `0-9`, `a-f`. +- `Hex_Digit` 包括 16 进制数字 `0-9`,`a-f`。 - ...And so on. +- ...等等 Unicode supports many different properties, their full list would require a lot of space, so here are the references: +Unicode 支持相当数量的属性,列出整个清单需要占用大量的空间,因此在这里列出相关的链接: - List all properties by a character: . +- 列出一个字符的所有属性 . - List all characters by a property: . +- 按照属性列出所有的字符 . - Short aliases for properties: . +- 属性的对应缩写形式: . - A full base of Unicode characters in text format, with all properties, is here: . +- 以文本格式整理的所有 Unicode 字符,包含了所有的属性: . ### Example: hexadecimal numbers From 6d724b4fdff63817c9119ed7e2b65843e8b0eab8 Mon Sep 17 00:00:00 2001 From: starshiro Date: Fri, 3 Jan 2020 20:48:33 +0800 Subject: [PATCH 3/6] Added translation for examples and summary --- .../03-regexp-unicode/article.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/9-regular-expressions/03-regexp-unicode/article.md b/9-regular-expressions/03-regexp-unicode/article.md index adb62dc179..1bf73ea51b 100644 --- a/9-regular-expressions/03-regexp-unicode/article.md +++ b/9-regular-expressions/03-regexp-unicode/article.md @@ -141,10 +141,13 @@ Unicode 支持相当数量的属性,列出整个清单需要占用大量的空 - 以文本格式整理的所有 Unicode 字符,包含了所有的属性: . ### Example: hexadecimal numbers +### 实例: 16 进制数字 For instance, let's look for hexadecimal numbers, written as `xFF`, where `F` is a hex digit (0..1 or A..F). +举个例子,让我们来查找 16 进制数字,写作 `xFF` 其中 `F` 是一个 16 进制的数字(0..1 或者 A..F)。 A hex digit can be denoted as `pattern:\p{Hex_Digit}`: +一个 16 进制数字可以表示为 `pattern:\p{Hex_Digit}`: ```js run let regexp = /x\p{Hex_Digit}\p{Hex_Digit}/u; @@ -153,12 +156,17 @@ alert("number: xAF".match(regexp)); // xAF ``` ### Example: Chinese hieroglyphs +### 实例: 中文字符 Let's look for Chinese hieroglyphs. +让我们再来考虑中文字符。 There's a unicode property `Script` (a writing system), that may have a value: `Cyrillic`, `Greek`, `Arabic`, `Han` (Chinese) and so on, [here's the full list]("https://en.wikipedia.org/wiki/Script_(Unicode)"). +有一个 unicode 属性 `Script` (一个书写系统),这个属性可以有一个值: `Cyrillic`,`Greek`,`Arabic`,`Han` (中文)等等, [这里是一个完整的列表]("https://en.wikipedia.org/wiki/Script_(Unicode)"). To look for characters in a given writing system we should use `pattern:Script=`, e.g. for Cyrillic letters: `pattern:\p{sc=Cyrillic}`, for Chinese hieroglyphs: `pattern:\p{sc=Han}`, and so on: +为了实现查找一个给定的书写系统中的字符,我们需要使用 `pattern:Script=`,例如对于 +西里尔字符: `pattern:\p{sc=Cyrillic}`, 中文字符: `pattern:\p{sc=Han}`,等等。 ```js run let regexp = /\p{sc=Han}/gu; // returns Chinese hieroglyphs @@ -169,10 +177,13 @@ alert( str.match(regexp) ); // 你,好 ``` ### Example: currency +### 实例: 货币 Characters that denote a currency, such as `$`, `€`, `¥`, have unicode property `pattern:\p{Currency_Symbol}`, the short alias: `pattern:\p{Sc}`. +表示货币的字符,例如 `$`,`€`,`¥`,具有 unicode 属性 `pattern:\p{Currency_Symbol}`,缩写为 `pattern:\p{Sc}`。 Let's use it to look for prices in the format "currency, followed by a digit": +让我们使用这一属性来查找符合“货币,接着是一个数字”的价格文本: ```js run let regexp = /\p{Sc}\d/gu; @@ -183,14 +194,21 @@ alert( str.match(regexp) ); // $2,€1,¥9 ``` Later, in the article we'll see how to look for numbers that contain many digits. +之后,在其它文章中 我们将会了解如何查找包含很多位的数字。 ## Summary +## 总结 Flag `pattern:u` enables the support of Unicode in regular expressions. +修饰符 `pattern:u` 在正则表达式中提供对 Unicode 的支持。 That means two things: +这意味着两件事: 1. Characters of 4 bytes are handled correctly: as a single character, not two 2-byte characters. +1. 4 个字节长的字符被以正确的方式处理: 看成单个的字符,而不是 2 个 2 字节长的字符。 2. Unicode properties can be used in the search: `\p{…}`. +2. Unicode 属性可以被用于查找中 `\p{…}`. With Unicode properties we can look for words in given languages, special characters (quotes, currencies) and so on. +有了 unicode 属性我们可以查找给定语言中的词,特殊字符(引用,货币)等等。 From 761beac236b1646df5e9b3f73cf63bf76ba6e440 Mon Sep 17 00:00:00 2001 From: starshiro Date: Fri, 3 Jan 2020 21:05:36 +0800 Subject: [PATCH 4/6] Minor modifications and removed original Enlighs text. --- .../03-regexp-unicode/article.md | 59 ++----------------- 1 file changed, 6 insertions(+), 53 deletions(-) diff --git a/9-regular-expressions/03-regexp-unicode/article.md b/9-regular-expressions/03-regexp-unicode/article.md index 1bf73ea51b..26933ea922 100644 --- a/9-regular-expressions/03-regexp-unicode/article.md +++ b/9-regular-expressions/03-regexp-unicode/article.md @@ -1,13 +1,9 @@ -# Unicode: flag "u" and class \p{...} # Unicode: 修饰符“u”和 class \p{...} -JavaScript uses [Unicode encoding](https://en.wikipedia.org/wiki/Unicode) for strings. Most characters are encoding with 2 bytes, but that allows to represent at most 65536 characters. -JavaScript 使用 [Unicode 编码](https://en.wikipedia.org/wiki/Unicode) (Unicode encoding)对字符串进行编码。但这种方式只能编码最多 65536 个字符。 +JavaScript 使用 [Unicode 编码](https://en.wikipedia.org/wiki/Unicode) (Unicode encoding)对字符串进行编码。大多数字符使用 2 个字节编码,但这种方式只能编码最多 65536 个字符。 -That range is not big enough to encode all possible characters, that's why some rare characters are encoded with 4 bytes, for instance like `𝒳` (mathematical X) or `😄` (a smile), some hieroglyphs and so on. 这个范围不足以对所有可能的字符进行编码,这就是为什么一些罕见的字符使用 4 个字节进行编码,比如 `𝒳` (数学符号 X)或者 `😄` (笑脸),一些象形文字等等。 -Here are the unicode values of some characters: 以下是一些字符对应的 unicode 编码: | 字符 | Unicode | unicode 中的字节数 | @@ -18,13 +14,10 @@ Here are the unicode values of some characters: | 𝒴 | `0x1d4b4` | 4 | | 😄 | `0x1f604` | 4 | -So characters like `a` and `≈` occupy 2 bytes, while codes for `𝒳`, `𝒴` and `😄` are longer, they have 4 bytes. 所以像 `a` 和 `≈` 这样的字符占用 2 个字节,而 `𝒳`,`𝒴` 和 `😄` 的对应编码则更长,它们具有 4 个字节的长度。 -Long time ago, when JavaScript language was created, Unicode encoding was simpler: there were no 4-byte characters. So, some language features still handle them incorrectly. 很久以前,当 JavaScript 被发明出来的时候,Unicode 的编码要更加简单:当时并没有 4 个字节长的字符。所以,一部分语言特性在现在仍旧对无法对 unicode 进行正确的处理。 -For instance, `length` thinks that here are two characters: 比如 `length` 认为这里的输入有 2 个字符: ```js run @@ -32,39 +25,28 @@ alert('😄'.length); // 2 alert('𝒳'.length); // 2 ``` -...But we can see that there's only one, right? The point is that `length` treats 4 bytes as two 2-byte characters. That's incorrect, because they must be considered only together (so-called "surrogate pair", you can read about them in the article ). ...但我们可以清楚地认识到输入的字符只有一个,对吧?关键在于 `length` 把 4 个字节当成了 2 个 2 字节长的字符。这是不对的,因为它们必须被当作一个整体来考虑。(即所谓的“代理伪字符”(surrogate pair),你可以在这里进一步阅读有关的的信息 )。 -By default, regular expressions also treat 4-byte "long characters" as a pair of 2-byte ones. And, as it happens with strings, that may lead to odd results. We'll see that a bit later, in the article . 默认情况下,正则表达式同样把一个 4 个字节的“长字符”当成一对 2 个字节长的字符。正如在字符串中遇到的情况,这将导致一些奇怪的结果。我们将很快在后面的文章中遇到 . -Unlike strings, regular expressions have flag `pattern:u` that fixes such problems. With such flag, a regexp handles 4-byte characters correctly. And also Unicode property search becomes available, we'll get to it next. 与字符串有所不同的是,正则表达式有一个修饰符 `pattern:u` 被用以解决此类问题。当一个正则表达式使用这个修饰符后,4 个字节长的字符将被正确地处理。同时也能够用上 Unicode 属性(Unicode property)来进行查找了。我们接下来就来了解这方面的内容。 -## Unicode properties \p{...} ## Unicode 属性(Unicode properties)\p{...} ```warn header="Not supported in Firefox and Edge" -Despite being a part of the standard since 2018, unicode proeprties are not supported in Firefox ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1361876)) and Edge ([bug](https://github.com/Microsoft/ChakraCore/issues/2969)). 尽管 unicode property 从 2018 年以来便作为标准的一部分, 但 unicode 属性在 Firefox ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1361876)) 和 Edge ([bug](https://github.com/Microsoft/ChakraCore/issues/2969)) 中并没有相应的支持。 -There's [XRegExp](http://xregexp.com) library that provides "extended" regular expressions with cross-browser support for unicode properties. 目前 [XRegExp](http://xregexp.com) 这个库提供“扩展”的正则表达式,其中包括对 unicode property 的跨平台支持。 ``` -Every character in Unicode has a lot of properties. They describe what "category" the character belongs to, contain miscellaneous information about it. Unicode 中的每一个字符都具有很多的属性。它们描述了一个字符属于哪个“类别”,包含了各种关于字符的信息。 -For instance, if a character has `Letter` property, it means that the character belongs to an alphabet (of any language). And `Number` property means that it's a digit: maybe Arabic or Chinese, and so on. 例如,如果一个字符具有 `Letter` 属性,这意味着这个字符归属于(任意语言的)一个字母表。而 `Number` 属性则表示这是一个数字:也许是阿拉伯语,亦或者是中文,等等。 -We can search for characters with a property, written as `pattern:\p{…}`. To use `pattern:\p{…}`, a regular expression must have flag `pattern:u`. 我们可以查找具有某种属性的字符,写作 `pattern:\p{…}`。为了顺利使用 `pattern:\p{…}`,一个正则表达式必须使用修饰符 `pattern:u`。 -For instance, `\p{Letter}` denotes a letter in any of language. We can also use `\p{L}`, as `L` is an alias of `Letter`. There are shorter aliases for almost every property. 举个例子,`\p{Letter}` 表示任何语言中的一个字母。我们也可以使用 `\p{L}`,因为 `L` 是 `Letter` 的一个别名(alias)。对于每种属性而言,几乎都存在对应的缩写别名。 -In the example below three kinds of letters will be found: English, Georgean and Korean. 在下面的例子中 3 种字母将会被查找出: 英语、格鲁吉亚语和韩语。 ```js run @@ -74,7 +56,6 @@ alert( str.match(/\p{L}/gu) ); // A,ბ,ㄱ alert( str.match(/\p{L}/g) ); // null(没有匹配的文本,因为没有修饰符“u”) ``` -Here's the main character categories and their subcategories: 以下是主要的字符类别和它们对应的子类别: - 字母(Letter) `L`: @@ -116,37 +97,24 @@ Here's the main character categories and their subcategories: - 代理伪字符(surrogate) `Cs`. -So, e.g. if we need letters in lower case, we can write `pattern:\p{Ll}`, punctuation signs: `pattern:\p{P}` and so on. -所以,比如说我们需要小写的字母,就可以写成 `pattern:\p{Ll}`,标点符号写作 `pattern:\p{P}` 等等。 +因此,比如说我们需要小写的字母,就可以写成 `pattern:\p{Ll}`,标点符号写作 `pattern:\p{P}` 等等。 -There are also other derived categories, like: 也有其它派生的类别,例如: -- `Alphabetic` (`Alpha`), includes Letters `L`, plus letter numbers `Nl` (e.g. Ⅻ - a character for the roman number 12), plus some other symbols `Other_Alphabetic` (`OAlpha`). - `Alphabetic` (`Alpha`), 包含了字母 `L`, 加上字母数字 `Nl` (例如 Ⅻ - 罗马数字 12),加上一些其它符号 `Other_Alphabetic` (`OAlpha`). -- `Hex_Digit` includes hexadecimal digits: `0-9`, `a-f`. - `Hex_Digit` 包括 16 进制数字 `0-9`,`a-f`。 -- ...And so on. - ...等等 -Unicode supports many different properties, their full list would require a lot of space, so here are the references: Unicode 支持相当数量的属性,列出整个清单需要占用大量的空间,因此在这里列出相关的链接: -- List all properties by a character: . - 列出一个字符的所有属性 . -- List all characters by a property: . - 按照属性列出所有的字符 . -- Short aliases for properties: . - 属性的对应缩写形式: . -- A full base of Unicode characters in text format, with all properties, is here: . - 以文本格式整理的所有 Unicode 字符,包含了所有的属性: . -### Example: hexadecimal numbers ### 实例: 16 进制数字 -For instance, let's look for hexadecimal numbers, written as `xFF`, where `F` is a hex digit (0..1 or A..F). 举个例子,让我们来查找 16 进制数字,写作 `xFF` 其中 `F` 是一个 16 进制的数字(0..1 或者 A..F)。 -A hex digit can be denoted as `pattern:\p{Hex_Digit}`: 一个 16 进制数字可以表示为 `pattern:\p{Hex_Digit}`: ```js run @@ -155,18 +123,13 @@ let regexp = /x\p{Hex_Digit}\p{Hex_Digit}/u; alert("number: xAF".match(regexp)); // xAF ``` -### Example: Chinese hieroglyphs ### 实例: 中文字符 -Let's look for Chinese hieroglyphs. 让我们再来考虑中文字符。 -There's a unicode property `Script` (a writing system), that may have a value: `Cyrillic`, `Greek`, `Arabic`, `Han` (Chinese) and so on, [here's the full list]("https://en.wikipedia.org/wiki/Script_(Unicode)"). -有一个 unicode 属性 `Script` (一个书写系统),这个属性可以有一个值: `Cyrillic`,`Greek`,`Arabic`,`Han` (中文)等等, [这里是一个完整的列表]("https://en.wikipedia.org/wiki/Script_(Unicode)"). +有一个 unicode 属性 `Script` (一个书写系统),这个属性可以有一个值: `Cyrillic`,`Greek`,`Arabic`,`Han` (中文)等等,[这里是一个完整的列表]("https://en.wikipedia.org/wiki/Script_(Unicode)"). -To look for characters in a given writing system we should use `pattern:Script=`, e.g. for Cyrillic letters: `pattern:\p{sc=Cyrillic}`, for Chinese hieroglyphs: `pattern:\p{sc=Han}`, and so on: -为了实现查找一个给定的书写系统中的字符,我们需要使用 `pattern:Script=`,例如对于 -西里尔字符: `pattern:\p{sc=Cyrillic}`, 中文字符: `pattern:\p{sc=Han}`,等等。 +为了实现查找一个给定的书写系统中的字符,我们需要使用 `pattern:Script=`,例如对于西里尔字符: `pattern:\p{sc=Cyrillic}`, 中文字符: `pattern:\p{sc=Han}`,等等。 ```js run let regexp = /\p{sc=Han}/gu; // returns Chinese hieroglyphs @@ -176,13 +139,10 @@ let str = `Hello Привет 你好 123_456`; alert( str.match(regexp) ); // 你,好 ``` -### Example: currency ### 实例: 货币 -Characters that denote a currency, such as `$`, `€`, `¥`, have unicode property `pattern:\p{Currency_Symbol}`, the short alias: `pattern:\p{Sc}`. 表示货币的字符,例如 `$`,`€`,`¥`,具有 unicode 属性 `pattern:\p{Currency_Symbol}`,缩写为 `pattern:\p{Sc}`。 -Let's use it to look for prices in the format "currency, followed by a digit": 让我们使用这一属性来查找符合“货币,接着是一个数字”的价格文本: ```js run @@ -193,22 +153,15 @@ let str = `Prices: $2, €1, ¥9`; alert( str.match(regexp) ); // $2,€1,¥9 ``` -Later, in the article we'll see how to look for numbers that contain many digits. -之后,在其它文章中 我们将会了解如何查找包含很多位的数字。 +之后,在文章 中我们将会了解如何查找包含很多位的数字。 -## Summary ## 总结 -Flag `pattern:u` enables the support of Unicode in regular expressions. 修饰符 `pattern:u` 在正则表达式中提供对 Unicode 的支持。 -That means two things: 这意味着两件事: -1. Characters of 4 bytes are handled correctly: as a single character, not two 2-byte characters. -1. 4 个字节长的字符被以正确的方式处理: 看成单个的字符,而不是 2 个 2 字节长的字符。 -2. Unicode properties can be used in the search: `\p{…}`. +1. 4 个字节长的字符被以正确的方式处理: 被看成单个的字符,而不是 2 个 2 字节长的字符。 2. Unicode 属性可以被用于查找中 `\p{…}`. -With Unicode properties we can look for words in given languages, special characters (quotes, currencies) and so on. 有了 unicode 属性我们可以查找给定语言中的词,特殊字符(引用,货币)等等。 From 984b3ee0e84adc019f7b348bfb014acb16c31f61 Mon Sep 17 00:00:00 2001 From: starshiro Date: Mon, 6 Jan 2020 22:10:21 +0800 Subject: [PATCH 5/6] Various fixes. - Fix some typos. - Replace all end-of-line Enlish periods with Chinese full stops. - Eliminate whitespace following Chinese punctuations. --- .../03-regexp-unicode/article.md | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/9-regular-expressions/03-regexp-unicode/article.md b/9-regular-expressions/03-regexp-unicode/article.md index 26933ea922..efd394fd11 100644 --- a/9-regular-expressions/03-regexp-unicode/article.md +++ b/9-regular-expressions/03-regexp-unicode/article.md @@ -1,4 +1,4 @@ -# Unicode: 修饰符“u”和 class \p{...} +# Unicode:修饰符 “u” 和 class \p{...} JavaScript 使用 [Unicode 编码](https://en.wikipedia.org/wiki/Unicode) (Unicode encoding)对字符串进行编码。大多数字符使用 2 个字节编码,但这种方式只能编码最多 65536 个字符。 @@ -16,7 +16,7 @@ JavaScript 使用 [Unicode 编码](https://en.wikipedia.org/wiki/Unicode) (Uni 所以像 `a` 和 `≈` 这样的字符占用 2 个字节,而 `𝒳`,`𝒴` 和 `😄` 的对应编码则更长,它们具有 4 个字节的长度。 -很久以前,当 JavaScript 被发明出来的时候,Unicode 的编码要更加简单:当时并没有 4 个字节长的字符。所以,一部分语言特性在现在仍旧对无法对 unicode 进行正确的处理。 +很久以前,当 JavaScript 被发明出来的时候,Unicode 的编码要更加简单:当时并没有 4 个字节长的字符。所以,一部分语言特性在现在仍旧无法对 unicode 进行正确的处理。 比如 `length` 认为这里的输入有 2 个字符: @@ -27,13 +27,13 @@ alert('𝒳'.length); // 2 ...但我们可以清楚地认识到输入的字符只有一个,对吧?关键在于 `length` 把 4 个字节当成了 2 个 2 字节长的字符。这是不对的,因为它们必须被当作一个整体来考虑。(即所谓的“代理伪字符”(surrogate pair),你可以在这里进一步阅读有关的的信息 )。 -默认情况下,正则表达式同样把一个 4 个字节的“长字符”当成一对 2 个字节长的字符。正如在字符串中遇到的情况,这将导致一些奇怪的结果。我们将很快在后面的文章中遇到 . +默认情况下,正则表达式同样把一个 4 个字节的“长字符”当成一对 2 个字节长的字符。正如在字符串中遇到的情况,这将导致一些奇怪的结果。我们将很快在后面的文章中遇到 。 与字符串有所不同的是,正则表达式有一个修饰符 `pattern:u` 被用以解决此类问题。当一个正则表达式使用这个修饰符后,4 个字节长的字符将被正确地处理。同时也能够用上 Unicode 属性(Unicode property)来进行查找了。我们接下来就来了解这方面的内容。 ## Unicode 属性(Unicode properties)\p{...} -```warn header="Not supported in Firefox and Edge" +```warn header="在 Firefox 和 Edge 中缺乏支持" 尽管 unicode property 从 2018 年以来便作为标准的一部分, 但 unicode 属性在 Firefox ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1361876)) 和 Edge ([bug](https://github.com/Microsoft/ChakraCore/issues/2969)) 中并没有相应的支持。 目前 [XRegExp](http://xregexp.com) 这个库提供“扩展”的正则表达式,其中包括对 unicode property 的跨平台支持。 @@ -47,7 +47,7 @@ Unicode 中的每一个字符都具有很多的属性。它们描述了一个字 举个例子,`\p{Letter}` 表示任何语言中的一个字母。我们也可以使用 `\p{L}`,因为 `L` 是 `Letter` 的一个别名(alias)。对于每种属性而言,几乎都存在对应的缩写别名。 -在下面的例子中 3 种字母将会被查找出: 英语、格鲁吉亚语和韩语。 +在下面的例子中 3 种字母将会被查找出:英语、格鲁吉亚语和韩语。 ```js run let str = "A ბ ㄱ"; @@ -61,13 +61,13 @@ alert( str.match(/\p{L}/g) ); // null(没有匹配的文本,因为没有修 - 字母(Letter) `L`: - 小写(lowercase) `Ll` - 修饰(modifier) `Lm`, - - 标题(titlecase) `Lt`, + - 首字母大小写(titlecase) `Lt`, - 大写(uppercase) `Lu`, - - 其它(other) `Lo`. + - 其它(other) `Lo`。 - 数字(Number) `N`: - 十进制数字(decimal digit) `Nd`, - 字母数字(letter number) `Nl`, - - 其它(other) `No`. + - 其它(other) `No`。 - 标点符号(Punctuation) `P`: - 链接符(connector) `Pc`, - 横杠(dash) `Pd`, @@ -75,32 +75,32 @@ alert( str.match(/\p{L}/g) ); // null(没有匹配的文本,因为没有修 - 结束引用号(final quote) `Pf`, - 开(open) `Ps`, - 闭(close) `Pe`, - - 其它(other) `Po`. + - 其它(other) `Po`。 - 标记(Mark) `M` (accents etc): - 间隔合并(spacing combining) `Mc`, - 封闭(enclosing) `Me`, - - 非间隔(non-spacing) `Mn`. + - 非间隔(non-spacing) `Mn`。 - 符号(Symbol) `S`: - 货币(currency) `Sc`, - 修饰(modifier) `Sk`, - 数学(math) `Sm`, - - 其它(other) `So`. + - 其它(other) `So`。 - 分隔符(Separator) `Z`: - 行(line) `Zl`, - 段落(paragraph) `Zp`, - - 空格(space) `Zs`. + - 空格(space) `Zs`。 - 其它(Other) `C`: - 控制符(control) `Cc`, - 格式(format) `Cf`, - 为分配(not assigned) `Cn`, - 私有(private use) `Co`, - - 代理伪字符(surrogate) `Cs`. + - 代理伪字符(surrogate) `Cs`。 因此,比如说我们需要小写的字母,就可以写成 `pattern:\p{Ll}`,标点符号写作 `pattern:\p{P}` 等等。 也有其它派生的类别,例如: -- `Alphabetic` (`Alpha`), 包含了字母 `L`, 加上字母数字 `Nl` (例如 Ⅻ - 罗马数字 12),加上一些其它符号 `Other_Alphabetic` (`OAlpha`). +- `Alphabetic` (`Alpha`), 包含了字母 `L`, 加上字母数字 `Nl` (例如 Ⅻ - 罗马数字 12),加上一些其它符号 `Other_Alphabetic` (`OAlpha`)。 - `Hex_Digit` 包括 16 进制数字 `0-9`,`a-f`。 - ...等等 @@ -108,10 +108,10 @@ Unicode 支持相当数量的属性,列出整个清单需要占用大量的空 - 列出一个字符的所有属性 . - 按照属性列出所有的字符 . -- 属性的对应缩写形式: . -- 以文本格式整理的所有 Unicode 字符,包含了所有的属性: . +- 属性的对应缩写形式:. +- 以文本格式整理的所有 Unicode 字符,包含了所有的属性:. -### 实例: 16 进制数字 +### 实例:16 进制数字 举个例子,让我们来查找 16 进制数字,写作 `xFF` 其中 `F` 是一个 16 进制的数字(0..1 或者 A..F)。 @@ -123,13 +123,13 @@ let regexp = /x\p{Hex_Digit}\p{Hex_Digit}/u; alert("number: xAF".match(regexp)); // xAF ``` -### 实例: 中文字符 +### 实例:中文字符 让我们再来考虑中文字符。 -有一个 unicode 属性 `Script` (一个书写系统),这个属性可以有一个值: `Cyrillic`,`Greek`,`Arabic`,`Han` (中文)等等,[这里是一个完整的列表]("https://en.wikipedia.org/wiki/Script_(Unicode)"). +有一个 unicode 属性 `Script` (一个书写系统),这个属性可以有一个值:`Cyrillic`,`Greek`,`Arabic`,`Han` (中文)等等,[这里是一个完整的列表]("https://en.wikipedia.org/wiki/Script_(Unicode)")。 -为了实现查找一个给定的书写系统中的字符,我们需要使用 `pattern:Script=`,例如对于西里尔字符: `pattern:\p{sc=Cyrillic}`, 中文字符: `pattern:\p{sc=Han}`,等等。 +为了实现查找一个给定的书写系统中的字符,我们需要使用 `pattern:Script=`,例如对于西里尔字符:`pattern:\p{sc=Cyrillic}`, 中文字符:`pattern:\p{sc=Han}`,等等。 ```js run let regexp = /\p{sc=Han}/gu; // returns Chinese hieroglyphs @@ -139,7 +139,7 @@ let str = `Hello Привет 你好 123_456`; alert( str.match(regexp) ); // 你,好 ``` -### 实例: 货币 +### 实例:货币 表示货币的字符,例如 `$`,`€`,`¥`,具有 unicode 属性 `pattern:\p{Currency_Symbol}`,缩写为 `pattern:\p{Sc}`。 @@ -161,7 +161,7 @@ alert( str.match(regexp) ); // $2,€1,¥9 这意味着两件事: -1. 4 个字节长的字符被以正确的方式处理: 被看成单个的字符,而不是 2 个 2 字节长的字符。 -2. Unicode 属性可以被用于查找中 `\p{…}`. +1. 4 个字节长的字符被以正确的方式处理:被看成单个的字符,而不是 2 个 2 字节长的字符。 +2. Unicode 属性可以被用于查找中 `\p{…}`。 有了 unicode 属性我们可以查找给定语言中的词,特殊字符(引用,货币)等等。 From 29aa278255be8a712f210c886c98c9c018c1bc13 Mon Sep 17 00:00:00 2001 From: starshiro Date: Mon, 6 Jan 2020 22:55:04 +0800 Subject: [PATCH 6/6] Fixed a couple of typos. --- 9-regular-expressions/03-regexp-unicode/article.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/9-regular-expressions/03-regexp-unicode/article.md b/9-regular-expressions/03-regexp-unicode/article.md index efd394fd11..1bbe21ffd8 100644 --- a/9-regular-expressions/03-regexp-unicode/article.md +++ b/9-regular-expressions/03-regexp-unicode/article.md @@ -33,7 +33,7 @@ alert('𝒳'.length); // 2 ## Unicode 属性(Unicode properties)\p{...} -```warn header="在 Firefox 和 Edge 中缺乏支持" +```warn header="在 Firefox 和 Edge 中缺乏支持" 尽管 unicode property 从 2018 年以来便作为标准的一部分, 但 unicode 属性在 Firefox ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1361876)) 和 Edge ([bug](https://github.com/Microsoft/ChakraCore/issues/2969)) 中并没有相应的支持。 目前 [XRegExp](http://xregexp.com) 这个库提供“扩展”的正则表达式,其中包括对 unicode property 的跨平台支持。 @@ -61,7 +61,7 @@ alert( str.match(/\p{L}/g) ); // null(没有匹配的文本,因为没有修 - 字母(Letter) `L`: - 小写(lowercase) `Ll` - 修饰(modifier) `Lm`, - - 首字母大小写(titlecase) `Lt`, + - 首字母大写(titlecase) `Lt`, - 大写(uppercase) `Lu`, - 其它(other) `Lo`。 - 数字(Number) `N`: @@ -92,7 +92,7 @@ alert( str.match(/\p{L}/g) ); // null(没有匹配的文本,因为没有修 - 其它(Other) `C`: - 控制符(control) `Cc`, - 格式(format) `Cf`, - - 为分配(not assigned) `Cn`, + - 未分配(not assigned) `Cn`, - 私有(private use) `Co`, - 代理伪字符(surrogate) `Cs`。