diff --git a/5-regular-expressions/12-regexp-anchors/1-start-end/solution.md b/5-regular-expressions/12-regexp-anchors/1-start-end/solution.md index 1a8cbe9a23..8c2a89b919 100644 --- a/5-regular-expressions/12-regexp-anchors/1-start-end/solution.md +++ b/5-regular-expressions/12-regexp-anchors/1-start-end/solution.md @@ -1,6 +1,6 @@ -The empty string is the only match: it starts and immediately finishes. +唯一一个匹配的字符串是空字符串:它的开始紧跟着结束。 -The task once again demonstrates that anchors are not characters, but tests. +这个题目再一次说明了锚不是一个字符串,而是一个测试。 -The string is empty `""`. The engine first matches the `pattern:^` (input start), yes it's there, and then immediately the end `pattern:$`, it's here too. So there's a match. +对于空字符串 `""`,正则表达式引擎将会首先匹配模式 `^`(输入开始),匹配成功之后,会紧跟着检查模式 `$`,也匹配成功。所以空字符串是匹配 `^$` 的。 diff --git a/5-regular-expressions/12-regexp-anchors/1-start-end/task.md b/5-regular-expressions/12-regexp-anchors/1-start-end/task.md index abdfec9381..fd465c8b8a 100644 --- a/5-regular-expressions/12-regexp-anchors/1-start-end/task.md +++ b/5-regular-expressions/12-regexp-anchors/1-start-end/task.md @@ -1,3 +1,3 @@ -# Regexp ^$ +# 正则表达式 ^$ -Which string matches the pattern `pattern:^$`? +什么字符串可以匹配模式 `^$`? diff --git a/5-regular-expressions/12-regexp-anchors/2-test-mac/solution.md b/5-regular-expressions/12-regexp-anchors/2-test-mac/solution.md index 422bc65e41..6ef375dd6a 100644 --- a/5-regular-expressions/12-regexp-anchors/2-test-mac/solution.md +++ b/5-regular-expressions/12-regexp-anchors/2-test-mac/solution.md @@ -1,21 +1,21 @@ -A two-digit hex number is `pattern:[0-9a-f]{2}` (assuming the `pattern:i` flag is enabled). +两位十六进制数的模式是 `[0-9a-f]{2}`(假设 `i` flag 已被启用)。 -We need that number `NN`, and then `:NN` repeated 5 times (more numbers); +我们需要一个 `NN` 这种形式的数字,后面还需要五个 `:NN` 形式的数字。 -The regexp is: `pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}` +最终的正则表达式是:`[0-9a-f]{2}(:[0-9a-f]{2}){5}` -Now let's show that the match should capture all the text: start at the beginning and end at the end. That's done by wrapping the pattern in `pattern:^...$`. +现在让我们看看此模式如何匹配整个文本:从 `^` 处开始,到 `$` 这里结束。通过将匹配模式包裹在 `^...$` 来完成的。 -Finally: +最终结果: ```js run let reg = /^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/i; alert( reg.test('01:32:54:67:89:AB') ); // true -alert( reg.test('0132546789AB') ); // false (no colons) +alert( reg.test('0132546789AB') ); // false(缺少冒号) -alert( reg.test('01:32:54:67:89') ); // false (5 numbers, need 6) +alert( reg.test('01:32:54:67:89') ); // false(只有 5 个数字,必须是 6 个数字) -alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ in the end) +alert( reg.test('01:32:54:67:89:ZZ') ) // false(ZZ 不是合法的十六进制) ``` diff --git a/5-regular-expressions/12-regexp-anchors/2-test-mac/task.md b/5-regular-expressions/12-regexp-anchors/2-test-mac/task.md index e72655984b..c0f7cfc674 100644 --- a/5-regular-expressions/12-regexp-anchors/2-test-mac/task.md +++ b/5-regular-expressions/12-regexp-anchors/2-test-mac/task.md @@ -1,20 +1,21 @@ -# Check MAC-address +# 检验 MAC 地址 -[MAC-address](https://en.wikipedia.org/wiki/MAC_address) of a network interface consists of 6 two-digit hex numbers separated by a colon. +作为互联网接口的 [MAC 地址](https://en.wikipedia.org/wiki/MAC_address) 包括了 6 个以冒号 `:` 分隔的两位十六进制数。 -For instance: `subject:'01:32:54:67:89:AB'`. +举个例子:`subject:'01:32:54:67:89:AB'`。 -Write a regexp that checks whether a string is MAC-address. +请写一个能检查所有 MAC 地址的正则表达式。 + +用法: -Usage: ```js let reg = /your regexp/; alert( reg.test('01:32:54:67:89:AB') ); // true -alert( reg.test('0132546789AB') ); // false (no colons) +alert( reg.test('0132546789AB') ); // false(缺少冒号) -alert( reg.test('01:32:54:67:89') ); // false (5 numbers, must be 6) +alert( reg.test('01:32:54:67:89') ); // false(只有 5 个数字,必须是 6 个数字) -alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ ad the end) +alert( reg.test('01:32:54:67:89:ZZ') ) // false(ZZ 不是合法的十六进制) ``` diff --git a/5-regular-expressions/12-regexp-anchors/article.md b/5-regular-expressions/12-regexp-anchors/article.md index b4981e094c..06623f9535 100644 --- a/5-regular-expressions/12-regexp-anchors/article.md +++ b/5-regular-expressions/12-regexp-anchors/article.md @@ -1,10 +1,10 @@ -# String start ^ and finish $ +# 字符串的开始符 ^ 和结束符 $ -The caret `pattern:'^'` and dollar `pattern:'$'` characters have special meaning in a regexp. They are called "anchors". +脱字符 `'^'` 和美元符 `'$'` 在正则表达式中有特殊的含义,它们被叫做 “锚”。 -The caret `pattern:^` matches at the beginning of the text, and the dollar `pattern:$` -- in the end. +脱字符 `^` 匹配文本的开始,而美元符 `$` 匹配文本的结束。 -For instance, let's test if the text starts with `Mary`: +举个例子,让我们来匹配以 `Mary`开头的文本: ```js run let str1 = "Mary had a little lamb, it's fleece was white as snow"; @@ -14,13 +14,13 @@ alert( /^Mary/.test(str1) ); // true alert( /^Mary/.test(str2) ); // false ``` -The pattern `pattern:^Mary` means: "the string start and then Mary". +这个正则表达式 `^Mary` 意味着(匹配的是):“字符串的开始然后是 Mary”。 -Now let's test whether the text ends with an email. +现在让我们匹配所有以一个 email 结尾的文本: -To match an email, we can use a regexp `pattern:[-.\w]+@([\w-]+\.)+[\w-]{2,20}`. It's not perfect, but mostly works. +为了去匹配一个 email,我们可以使用 `pattern:[-.\w]+@([\w-]+\.)+[\w-]{2,20}` 这个正则表达式,它不是完美的,但是大多数情况下都可以正常工作。 -To test whether the string ends with the email, let's add `pattern:$` to the pattern: +为了匹配以 email 结尾的文本,让我们给这个正则表达式添加一个 `$` 吧: ```js run let reg = /[-.\w]+@([\w-]+\.)+[\w-]{2,20}$/g; @@ -32,11 +32,11 @@ alert( reg.test(str1) ); // true alert( reg.test(str2) ); // false ``` -We can use both anchors together to check whether the string exactly follows the pattern. That's often used for validation. +我们可以同时使用这两个符号,来检查字符串是不是完全匹配正则表达式。这经常用于信息校验。 -For instance we want to check that `str` is exactly a color in the form `#` plus 6 hex digits. The pattern for the color is `pattern:#[0-9a-f]{6}`. +举个例子,我们想去确认一个 `字符串` 是以 `#` 加上6个十六进制数字所表示颜色。这个颜色字符串的模式是 `pattern:#[0-9a-f]{6}`。 -To check that the *whole string* exactly matches it, we add `pattern:^...$`: +要检查 *整个字符串* 完全匹配正则表达式的模式,我们需要加上 `^...$`: ```js run let str = "#abcdef"; @@ -44,12 +44,12 @@ let str = "#abcdef"; alert( /^#[0-9a-f]{6}$/i.test(str) ); // true ``` -The regexp engine looks for the text start, then the color, and then immediately the text end. Just what we need. +正则表达式的引擎会先找到文本的开始,然后匹配颜色字符串,然后查看文本是否在字符串之后立即结束。这样子正是我们所想要的。 -```smart header="Anchors have zero length" -Anchors just like `\b` are tests. They have zero-width. +```smart header="锚的长度为零" +锚就像 `\b` 一样,是测试时的一个字符,他们没有长度。 -In other words, they do not match a character, but rather force the regexp engine to check the condition (text start/end). +换句话说,它们不会检验到一个字符,但是它们会让正则表达式的引擎去检查判定条件(例如是否处于文本开头/结尾)。 ``` -The behavior of anchors changes if there's a flag `pattern:m` (multiline mode). We'll explore it in the next chapter. +当正则表达式有一个 `m`(多行模式)flag 时,这些锚的性质会发生改变。我们将会在下一章讨论它。 diff --git a/5-regular-expressions/13-regexp-multiline-mode/article.md b/5-regular-expressions/13-regexp-multiline-mode/article.md index 1b5623dc8e..25ff611428 100644 --- a/5-regular-expressions/13-regexp-multiline-mode/article.md +++ b/5-regular-expressions/13-regexp-multiline-mode/article.md @@ -1,14 +1,14 @@ -# Multiline mode, flag "m" +# Flag "m" — 多行模式 -The multiline mode is enabled by the flag `pattern:/.../m`. +通过 flag `/.../m` 可以开启多行模式。 -It only affects the behavior of `pattern:^` and `pattern:$`. +这仅仅会影响 `^` 和 `$` 锚符的行为。 -In the multiline mode they match not only at the beginning and end of the string, but also at start/end of line. +在多行模式下,它们不仅仅匹配文本的开始与结束,还匹配每一行的开始与结束。 -## Line start ^ +## 行的开头 ^ -In the example below the text has multiple lines. The pattern `pattern:/^\d+/gm` takes a number from the beginning of each one: +在这个有多行文本的例子中,正则表达式 `/^\d+/gm` 将匹配每一行的开头数字: ```js run let str = `1st place: Winnie @@ -20,7 +20,7 @@ alert( str.match(/^\d+/gm) ); // 1, 2, 33 */!* ``` -Without the flag `pattern:/.../m` only the first number is matched: +没有 flag `/.../m` 时,仅仅是第一个数字被匹配到: ```js run @@ -33,15 +33,15 @@ alert( str.match(/^\d+/g) ); // 1 */!* ``` -That's because by default a caret `pattern:^` only matches at the beginning of the text, and in the multiline mode -- at the start of a line. +这是因为默认情况下,锚符 `^` 仅仅匹配文本的开头,在多行模式下,它匹配行的开头。 -The regular expression engine moves along the text and looks for a string start `pattern:^`, when finds -- continues to match the rest of the pattern `pattern:\d+`. +正则表达式引擎将会在文本中查找以锚符 `^` 开始的字符串,我们找到之后继续匹配 `\d+` 模式。 -## Line end $ +## 行的结尾 $ -The dollar sign `pattern:$` behaves similarly. +美元符 `$` 行为也相似。 -The regular expression `pattern:\w+$` finds the last word in every line +正则表达式 `\w+$ 会找到每一行的最后一个单词: ```js run let str = `1st place: Winnie @@ -51,15 +51,15 @@ let str = `1st place: Winnie alert( str.match(/\w+$/gim) ); // Winnie,Piglet,Eeyore ``` -Without the `pattern:/.../m` flag the dollar `pattern:$` would only match the end of the whole string, so only the very last word would be found. +没有 `/.../m` flag 的话,美元符 `$` 将会仅仅匹配整个文本的结尾,所以只有最后的一个单词会被找到。 -## Anchors ^$ versus \n +## 锚符 ^$ 对比 \n -To find a newline, we can use not only `pattern:^` and `pattern:$`, but also the newline character `\n`. +要寻找新的一行的话,我们不仅可以使用锚符 `^` 和 `$`,也可以使用批匹配符 `\n`。 -The first difference is that unlike anchors, the character `\n` "consumes" the newline character and adds it to the result. +它和锚符 `^` 和 `$` 的第一个不同点是它不像锚符那样,它会“消耗”掉 `\n` 并且将其(`\n`)加入到匹配结果中。 -For instance, here we use it instead of `pattern:$`: +举个例子,我们在下面的代码中用它来替代 `$`: ```js run let str = `1st place: Winnie @@ -69,8 +69,8 @@ let str = `1st place: Winnie alert( str.match(/\w+\n/gim) ); // Winnie\n,Piglet\n ``` -Here every match is a word plus a newline character. +这里,我们每次匹配到的时候都会被添加一个换行符。 -And one more difference -- the newline `\n` does not match at the string end. That's why `Eeyore` is not found in the example above. +还有一个不同点——换行符 `\n` 不会匹配字符串结尾。这就是为什么在上面的例子中 `Eeyore` 没有匹配到。 -So, anchors are usually better, they are closer to what we want to get. +所以,通常情况下使用锚符更棒,用它匹配出来的结果更加接近我们想要的结果。 diff --git a/5-regular-expressions/14-regexp-lookahead/article.md b/5-regular-expressions/14-regexp-lookahead/article.md index 70484b7d51..13ea5560d3 100644 --- a/5-regular-expressions/14-regexp-lookahead/article.md +++ b/5-regular-expressions/14-regexp-lookahead/article.md @@ -1,3 +1,3 @@ -# Lookahead (in progress) +# 前瞻断言(正在进行) -The article is under development, will be here when it's ready. +这篇文章有待完成,它会在其完成后展示在这里。 diff --git a/5-regular-expressions/index.md b/5-regular-expressions/index.md index ac25aaa678..8a1c9cb689 100644 --- a/5-regular-expressions/index.md +++ b/5-regular-expressions/index.md @@ -1,3 +1,3 @@ -# Regular expressions +# 正则表达式 -Regular expressions is a powerful way of doing search and replace in strings. +正则表达式是一个查找和替换字符串的强有力的工具。