diff --git a/5-regular-expressions/07-regexp-quantifiers/1-find-text-manydots/solution.md b/5-regular-expressions/07-regexp-quantifiers/1-find-text-manydots/solution.md index d4ddb13693..0aabba0dee 100644 --- a/5-regular-expressions/07-regexp-quantifiers/1-find-text-manydots/solution.md +++ b/5-regular-expressions/07-regexp-quantifiers/1-find-text-manydots/solution.md @@ -6,4 +6,4 @@ let reg = /\.{3,}/g; alert( "Hello!... How goes?.....".match(reg) ); // ..., ..... ``` -Please note that the dot is a special character, so we have to escape it and insert as `\.`. +需要注意的是,点号(.)是一个特殊字符,因此我们需要将其转义并作为 `\.` 插入语句。 diff --git a/5-regular-expressions/07-regexp-quantifiers/1-find-text-manydots/task.md b/5-regular-expressions/07-regexp-quantifiers/1-find-text-manydots/task.md index 6fd91bdcf3..33c516a6ce 100644 --- a/5-regular-expressions/07-regexp-quantifiers/1-find-text-manydots/task.md +++ b/5-regular-expressions/07-regexp-quantifiers/1-find-text-manydots/task.md @@ -2,13 +2,13 @@ importance: 5 --- -# How to find an ellipsis "..." ? +# 如何找到省略号 "..."? -Create a regexp to find ellipsis: 3 (or more?) dots in a row. +创建一个正则表达式来查找省略号:连续 3(或更多)个点。 -Check it: +例如: ```js -let reg = /your regexp/g; +let reg = /你的正则表达式/g; alert( "Hello!... How goes?.....".match(reg) ); // ..., ..... ``` diff --git a/5-regular-expressions/07-regexp-quantifiers/2-find-html-colors-6hex/solution.md b/5-regular-expressions/07-regexp-quantifiers/2-find-html-colors-6hex/solution.md index ec871d05c3..2f206df3f8 100644 --- a/5-regular-expressions/07-regexp-quantifiers/2-find-html-colors-6hex/solution.md +++ b/5-regular-expressions/07-regexp-quantifiers/2-find-html-colors-6hex/solution.md @@ -1,10 +1,10 @@ -We need to look for `#` followed by 6 hexadimal characters. +我们需要寻找 `#` 字符,后跟六个十六进制字符。 -A hexadimal character can be described as `pattern:[0-9a-fA-F]`. Or if we use the `i` flag, then just `pattern:[0-9a-f]`. +一个十六进制字符可以被描述为 `pattern:[0-9a-fA-F]`。如果我们使用 `i` 标识,那么只需要 `pattern:[0-9a-f]`。 -Then we can look for 6 of them using the quantifier `pattern:{6}`. +然后我们可以用量词 `pattern:{6}` 来查找这六个。 -As a result, we have the regexp: `pattern:/#[a-f0-9]{6}/gi`. +因此,我们得到正则表达式:`pattern:/#[a-f0-9]{6}/gi`。 ```js run let reg = /#[a-f0-9]{6}/gi; @@ -14,13 +14,13 @@ let str = "color:#121212; background-color:#AA00ef bad-colors:f#fddee #fd2" alert( str.match(reg) ); // #121212,#AA00ef ``` -The problem is that it finds the color in longer sequences: +问题是匹配到颜色值过长: ```js run alert( "#12345678".match( /#[a-f0-9]{6}/gi ) ) // #12345678 ``` -To fix that, we can add `pattern:\b` to the end: +为了解决这个问题,我们可以在末尾加上 `pattern:\b`: ```js run // color diff --git a/5-regular-expressions/07-regexp-quantifiers/2-find-html-colors-6hex/task.md b/5-regular-expressions/07-regexp-quantifiers/2-find-html-colors-6hex/task.md index 1960a09c61..19b0046284 100644 --- a/5-regular-expressions/07-regexp-quantifiers/2-find-html-colors-6hex/task.md +++ b/5-regular-expressions/07-regexp-quantifiers/2-find-html-colors-6hex/task.md @@ -1,15 +1,15 @@ -# Regexp for HTML colors +# 针对 HTML 颜色的正则表达式 -Create a regexp to search HTML-colors written as `#ABCDEF`: first `#` and then 6 hexadimal characters. +创建一个正则表达式来搜寻格式为 `#ABCDEF` 的 HTML 颜色值:首个字符 `#` 以及接下来的六位十六进制字符。 -An example of use: +一个例子: ```js -let reg = /...your regexp.../ +let reg = /...你的正则表达式.../ let str = "color:#121212; background-color:#AA00ef bad-colors:f#fddee #fd2 #12345678"; alert( str.match(reg) ) // #121212,#AA00ef ``` -P.S. In this task we do not need other color formats like `#123` or `rgb(1,2,3)` etc. +P.S. 在这个任务中,我们不需要其他的颜色格式,比如 `#123` 或 `rgb(1,2,3)` 等。 diff --git a/5-regular-expressions/07-regexp-quantifiers/article.md b/5-regular-expressions/07-regexp-quantifiers/article.md index 6b6933bb4a..a5583869af 100644 --- a/5-regular-expressions/07-regexp-quantifiers/article.md +++ b/5-regular-expressions/07-regexp-quantifiers/article.md @@ -1,40 +1,40 @@ -# Quantifiers +, *, ? and {n} +# 量词 `+,*,?` 和 `{n}` -Let's say we have a string like `+7(903)-123-45-67` and want to find all numbers in it. But unlike before, we are interested in not digits, but full numbers: `7, 903, 123, 45, 67`. +假设我们有一个字符串 `+7(903)-123-45-67`,并且想要找到它包含的所有数字。但与之前不同的是,我们对单个数字不感兴趣,只对全数感兴趣:`7, 903, 123, 45, 67`。 -A number is a sequence of 1 or more digits `\d`. The instrument to say how many we need is called *quantifiers*. +数字是一个或多个 `\d` 的序列。用来形容我们所需要的数量的词被称为**量词**。 -## Quantity {n} +## 数量 {n} -The most obvious quantifier is a number in figure quotes: `pattern:{n}`. A quantifier is put after a character (or a character class and so on) and specifies exactly how many we need. +最明显的量词便是一对引号间的数字:`pattern:{n}`。在一个字符(或一个字符类等等)后跟着一个量词,用来指出我们具体需要的数量。 -It also has advanced forms, here we go with examples: +它有更高级的格式,用一个例子来说明: -Exact count: `{5}` -: `pattern:\d{5}` denotes exactly 5 digits, the same as `pattern:\d\d\d\d\d`. +确切的位数:`{5}` +: `pattern:\d{5}` 表示 5 位的数字,如同 `pattern:\d\d\d\d\d`。 - The example below looks for a 5-digit number: + 接下来的例子将会查找一个五位数的数字: ```js run alert( "I'm 12345 years old".match(/\d{5}/) ); // "12345" ``` - We can add `\b` to exclude longer numbers: `pattern:\b\d{5}\b`. + 我们可以添加 `\b` 来排除更多位数的数字:`pattern:\b\d{5}\b`。 -The count from-to: `{3,5}` -: To find numbers from 3 to 5 digits we can put the limits into figure brackets: `pattern:\d{3,5}` +某个范围的位数:`{3,5}` +: 我们可以将限制范围的数字放入括号中,来查找位数为 3 至 5 位的数字:`pattern:\d{3,5}` ```js run alert( "I'm not 12, but 1234 years old".match(/\d{3,5}/) ); // "1234" ``` - We can omit the upper limit. Then a regexp `pattern:\d{3,}` looks for numbers of `3` and more digits: + 我们可以省略上限。那么正则表达式 `pattern:\d{3,}` 就会查找位数大于或等于 3 的数字: ```js run alert( "I'm not 12, but 345678 years old".match(/\d{3,}/) ); // "345678" ``` -In case with the string `+7(903)-123-45-67` we need numbers: one or more digits in a row. That is `pattern:\d{1,}`: +对于字符串 `+7(903)-123-45-67` 来说,我们如果需要一个或多个连续的数字,就使用 `pattern:\d{1,}`: ```js run let str = "+7(903)-123-45-67"; @@ -44,14 +44,14 @@ let numbers = str.match(/\d{1,}/g); alert(numbers); // 7,903,123,45,67 ``` -## Shorthands +## 缩写 -Most often needed quantifiers have shorthands: +大多数常用的量词都可以有缩写: `+` -: Means "one or more", the same as `{1,}`. +: 代表“一个或多个”,相当于 `{1,}`。 - For instance, `pattern:\d+` looks for numbers: + 例如,`pattern:\d+` 用来查找所有数字: ```js run let str = "+7(903)-123-45-67"; @@ -60,11 +60,11 @@ Most often needed quantifiers have shorthands: ``` `?` -: Means "zero or one", the same as `{0,1}`. In other words, it makes the symbol optional. +: 代表“零个或一个”,相当于 `{0,1}`。换句话说,它使得符号变得可选。 - For instance, the pattern `pattern:ou?r` looks for `match:o` followed by zero or one `match:u`, and then `match:r`. + 例如,模式 `pattern:ou?r` 查找 `match:o`,后跟零个或一个 `match:u`,然后是 `match:r`。 - So it can find `match:or` in the word `subject:color` and `match:our` in `subject:colour`: + 所以他能够在 `subject:color` 中找到 `match:or`,以及在 `subject:colour` 中找到 `match:our`: ```js run let str = "Should I write color or colour?"; @@ -73,61 +73,61 @@ Most often needed quantifiers have shorthands: ``` `*` -: Means "zero or more", the same as `{0,}`. That is, the character may repeat any times or be absent. +:代表着“零个或多个”,相当于 `{0,}`。也就是说,这个字符可以多次出现或不出现。 - The example below looks for a digit followed by any number of zeroes: + 接下来的例子将要寻找一个后跟任意数量的 0 的数字: ```js run alert( "100 10 1".match(/\d0*/g) ); // 100, 10, 1 ``` - Compare it with `'+'` (one or more): + 将它与 `'+'`(一个或多个)作比较: ```js run alert( "100 10 1".match(/\d0+/g) ); // 100, 10 ``` -## More examples +## 更多示例 -Quantifiers are used very often. They are one of the main "building blocks" for complex regular expressions, so let's see more examples. +量词是经常被使用的。它们是构成复杂的正则表达式的主要模块之一,我们接着来看更多的例子。 -Regexp "decimal fraction" (a number with a floating point): `pattern:\d+\.\d+` -: In action: +正则表达式“浮点数”(带浮点的数字):`pattern:\d+\.\d+` +: 实现: ```js run alert( "0 1 12.345 7890".match(/\d+\.\d+/g) ); // 12.345 ``` -Regexp "open HTML-tag without attributes", like `` or `

`: `pattern:/<[a-z]+>/i` -: In action: +正则表达式“打开没有属性的 HTML 标记”,比如 `` 或 `

`:`pattern:/<[a-z]+>/i` +: 实现: ```js run alert( " ... ".match(/<[a-z]+>/gi) ); // ``` - We look for character `pattern:'<'` followed by one or more English letters, and then `pattern:'>'`. + 我们查找字符 `pattern:'<'` 后跟一个或多个英文字母,然后是 `pattern:'>'`。 -Regexp "open HTML-tag without attributes" (improved): `pattern:/<[a-z][a-z0-9]*>/i` -: Better regexp: according to the standard, HTML tag name may have a digit at any position except the first one, like `

`. +正则表达式“打开没有属性的HTML标记”(改进版):`pattern:/<[a-z][a-z0-9]*>/i` +: 更好的表达式:根据标准,HTML 标记名称可以在除了第一个位置以外的任意一个位置有一个数字,比如 `

`。 ```js run alert( "

Hi!

".match(/<[a-z][a-z0-9]*>/gi) ); //

``` -Regexp "opening or closing HTML-tag without attributes": `pattern:/<\/?[a-z][a-z0-9]*>/i` -: We added an optional slash `pattern:/?` before the tag. Had to escape it with a backslash, otherwise JavaScript would think it is the pattern end. +正则表达式“打开没有属性的HTML标记”:`pattern:/<\/?[a-z][a-z0-9]*>/i` +: 我们在标记前加上了一个可选的斜杆 `pattern:/?`。必须用一个反斜杠来转义它,否则 JavaScript 就会认为它是这个模式的结束符。 ```js run alert( "

Hi!

".match(/<\/?[a-z][a-z0-9]*>/gi) ); //

,

``` -```smart header="More precise means more complex" -We can see one common rule in these examples: the more precise is the regular expression -- the longer and more complex it is. +```smart header="更精确意味着更复杂" +我们能够从这些例子中看到一个共同的规则:正则表达式越精确 —— 它就越长且越复杂。 -For instance, HTML tags could use a simpler regexp: `pattern:<\w+>`. +例如,HTML 标记能用一个简单的正则表达式:`pattern:<\w+>`。 -Because `pattern:\w` means any English letter or a digit or `'_'`, the regexp also matches non-tags, for instance `match:<_>`. But it's much simpler than `pattern:<[a-z][a-z0-9]*>`. +因为 `pattern:\w` 代表任意英文字母或数字或 `'_'`,这个正则表达式也能够匹配非标注的内容,比如 `match:<_>`。但它要比 `pattern:<[a-z][a-z0-9]*>` 简单很多。 -Are we ok with `pattern:<\w+>` or we need `pattern:<[a-z][a-z0-9]*>`? +我们能够接受 `pattern:<\w+>` 或者我们需要 `pattern:<[a-z][a-z0-9]*>`? -In real life both variants are acceptable. Depends on how tolerant we can be to "extra" matches and whether it's difficult or not to filter them out by other means. +在现实生活中,两种方式都能接受。取决于我们对于“额外”匹配的宽容程度以及是否难以通过其他方式来过滤掉它们。 ```