From d4b0ed892ae77b24dbbd7fde53e793aa2962400d Mon Sep 17 00:00:00 2001 From: coderming Date: Sun, 21 Oct 2018 14:05:43 +0800 Subject: [PATCH 1/8] 5-regular-expressions/12-regexp-anchors, 3-regexp-multiline-mode/article.md, 14-regexp-lookahead/article.md and 5-regular-expressions/index.md --- .../12-regexp-anchors/1-start-end/solution.md | 7 ++-- .../12-regexp-anchors/1-start-end/task.md | 4 +- .../12-regexp-anchors/2-test-mac/solution.md | 21 +++++----- .../12-regexp-anchors/2-test-mac/task.md | 17 ++++---- .../12-regexp-anchors/article.md | 32 +++++++-------- .../13-regexp-multiline-mode/article.md | 40 +++++++++---------- .../14-regexp-lookahead/article.md | 4 +- 5-regular-expressions/index.md | 4 +- 8 files changed, 65 insertions(+), 64 deletions(-) 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..b2962ac8b4 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,7 @@ +唯一一个匹配的字符串是空字符串:它的开始紧跟着结束。 -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..32669e0b4a 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,20 @@ -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}` +最终的正则表达式是:`pattern:[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; +最终结果: +```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, 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..3c4df24963 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..c5af61284e 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..26d0420918 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. +正则表达式是一个查找和替换字符串的强有力的工具。 From e7d861338f82a29f6ac03138464975edc8a2d9e0 Mon Sep 17 00:00:00 2001 From: coderming Date: Sun, 28 Oct 2018 21:04:40 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=AF=91=E8=80=85?= =?UTF-8?q?=E5=BB=BA=E8=AE=AE=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../12-regexp-anchors/1-start-end/solution.md | 4 ++-- .../12-regexp-anchors/2-test-mac/solution.md | 12 ++++++------ .../12-regexp-anchors/2-test-mac/task.md | 4 ++-- 5-regular-expressions/12-regexp-anchors/article.md | 10 +++++----- .../13-regexp-multiline-mode/article.md | 14 +++++++------- 5 files changed, 22 insertions(+), 22 deletions(-) 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 b2962ac8b4..47ce0d7685 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 @@ -2,6 +2,6 @@ -这个题目再一次展示了锚不是一个字符串,但是意味着一个测试。 +这个题目再一次说明了锚不是一个字符串,而是一个测试。 -最迂空字符串 `""`,正则表达式引擎将会首先匹配模式 `^` (输入开始),好的它在那里,然后正则表达式引擎将会紧跟着检查模式 `$`,噢!它也在那里。所以空字符串是匹配该模式的。 +对于空字符串 `""`,正则表达式引擎将会首先匹配模式 `^` (输入开始),匹配成功之后,会紧跟着检查模式 `$`,也匹配成功。所以空字符串是匹配 `^$` 的。 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 32669e0b4a..bf626d7224 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,20 +1,20 @@ 两位十六进制数的模式是 `[0-9a-f]{2}`(假设 `i` flag 已被启用)。 -我们需要一个 `NN` 这种形状的数字,和五个 `:NN` 形状的数字。 +我们需要一个 `NN` 这种形式的数字,后面还需要五个 `:NN` 形式的数字。 -最终的正则表达式是:`pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}` +最终的正则表达式是:`[0-9a-f]{2}(:[0-9a-f]{2}){5}` -现在让我们声明此模式应该匹配整个文本:在(正则表达式)开始的时候开始,在(正则表达式)结束的时候结束。让我么能通过包裹一个 `^...$` 的模式来实现这个功能。 +现在让我们看看此模式如何匹配整个文本:从 `^` 处开始,到 `$` 这里结束。通过将匹配模式包裹在 `^...$` 来完成的。 最终结果: ```js -let reg = /your regexp/; +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 (没有间隔) +alert( reg.test('0132546789AB') ); // false (缺少冒号) alert( reg.test('01:32:54:67:89') ); // false (只有5个数字,必须是6个数字) -alert( reg.test('01:32:54:67:89:ZZ') ) // false (最后面的数字是ZZ) +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 3c4df24963..29f86f18b3 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 @@ -12,10 +12,10 @@ let reg = /your regexp/; alert( reg.test('01:32:54:67:89:AB') ); // true -alert( reg.test('0132546789AB') ); // false (没有间隔) +alert( reg.test('0132546789AB') ); // false (缺少冒号) alert( reg.test('01:32:54:67:89') ); // false (只有5个数字,必须是6个数字) -alert( reg.test('01:32:54:67:89:ZZ') ) // false (最后面的数字是ZZ) +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 c5af61284e..625524a4fe 100644 --- a/5-regular-expressions/12-regexp-anchors/article.md +++ b/5-regular-expressions/12-regexp-anchors/article.md @@ -2,7 +2,7 @@ 脱字符 `'^'` 和美元符 `'$'` 在正则表达式中有特殊的含义,它们被叫做 “锚”。 -脱字符 `^` 匹配文本的开始,而美元符 `$`——匹配文本的结束。 +脱字符 `^` 匹配文本的开始,而美元符 `$` 匹配文本的结束。 举个例子,让我们来匹配以 `Mary`开头的文本: @@ -32,11 +32,11 @@ alert( reg.test(str1) ); // true alert( reg.test(str2) ); // false ``` -我们可以同时使用这两个符号来去检查字符串是不是完全匹配正则表达式的模式。这经常用于信息校验。 +我们可以同时使用这两个符号,来检查字符串是不是完全匹配正则表达式。这经常用于信息校验。 举个例子,我们想去确认一个 `字符串` 是以 `#` 加上6个十六进制数字所表示颜色。这个颜色字符串的模式是 `pattern:#[0-9a-f]{6}`。 -要检查 *整个字符串* 完全匹配正则表达式的模式,我们用添加 `^...$` 的方法: +要检查 *整个字符串* 完全匹配正则表达式的模式,我们需要加上 `^...$`: ```js run let str = "#abcdef"; @@ -44,12 +44,12 @@ let str = "#abcdef"; alert( /^#[0-9a-f]{6}$/i.test(str) ); // true ``` -正则表达式的引擎将会先校验文本的开始,然后是颜色字符串,最后立刻校验文本的结束。这样子正是我们所想要的。 +正则表达式的引擎会先找到文本的开始,然后匹配颜色字符串,然后查看文本是否在字符串之后立即结束。这样子正是我们所想要的。 ```smart header="锚的长度为零" 锚就像 `\b` 一样,是测试时的一个字符,他们没有长度。 -换句话说,它们不会检验到一个字符,但是它们会去限制正则表达式的引擎去检查(除字符外的)其它性质(例如文本开头/结尾)。 +换句话说,它们不会检验到一个字符,但是它们会让正则表达式的引擎去检查判定条件(例如是否处于文本开头/结尾)。 ``` 这些锚的性质会在正则表达式有一个 `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 26d0420918..781fac7d7a 100644 --- a/5-regular-expressions/13-regexp-multiline-mode/article.md +++ b/5-regular-expressions/13-regexp-multiline-mode/article.md @@ -1,4 +1,4 @@ -# Flag "m"——多行模式 +# Flag "m"—— 多行模式 通过 flag `/.../m` 可以开启多行模式。 @@ -8,7 +8,7 @@ ## 行的开头 ^ -在这个有多行文本的例子中,正则表达式`/^\d+/gm` 将匹配每一行的开头数字: +在这个有多行文本的例子中,正则表达式 `/^\d+/gm` 将匹配每一行的开头数字: ```js run let str = `1st place: Winnie @@ -33,9 +33,9 @@ alert( str.match(/^\d+/g) ); // 1 */!* ``` -这是因为默认情况下,锚符 `^` 仅仅匹配文本的开头,在多行模式下——它匹配行的开头。 +这是因为默认情况下,锚符 `^` 仅仅匹配文本的开头,在多行模式下,它匹配行的开头。 -正则表达式引擎将会在文本中查找以锚符 `^` 开始的字符串,我们找到了——然后接下来继续匹配 `\d+` 模式。 +正则表达式引擎将会在文本中查找以锚符 `^` 开始的字符串,我们找到之后继续匹配 `\d+` 模式。 ## 行的结尾 $ @@ -55,9 +55,9 @@ alert( str.match(/\w+$/gim) ); // Winnie,Piglet,Eeyore ## 锚符 ^$ 对比 \n -要寻找新的一行的话,我们不仅可以使用锚符`^` 和 `$`,也可以使用批匹配符 `\n`。 +要寻找新的一行的话,我们不仅可以使用锚符 `^` 和 `$`,也可以使用批匹配符 `\n`。 -它和锚符`^` 和 `$` 的第一个不同点是它不像锚符那样,它会“消耗”掉 `\n` 并且将其(`\n`)加入到匹配结果中。 +它和锚符 `^` 和 `$` 的第一个不同点是它不像锚符那样,它会“消耗”掉 `\n` 并且将其(`\n`)加入到匹配结果中。 举个例子,我们在下面的代码中用它来替代 `$`: @@ -73,4 +73,4 @@ alert( str.match(/\w+\n/gim) ); // Winnie\n,Piglet\n 还有一个不同点——换行符 `\n` 不会匹配字符串结尾。这就是为什么在上面的例子中 `Eeyore` 没有匹配到。 -所以,通常情况下使用锚符更棒,用它匹配出来的结果更加像我们想要的结果。 +所以,通常情况下使用锚符更棒,用它匹配出来的结果更加接近我们想要的结果。 From cd8bde42b4afb2b92c623b947a653cf9e98f3953 Mon Sep 17 00:00:00 2001 From: LeviDing Date: Tue, 30 Oct 2018 11:06:33 +0800 Subject: [PATCH 3/8] Update solution.md --- .../12-regexp-anchors/1-start-end/solution.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 47ce0d7685..9ba96f61c6 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,5 @@ -唯一一个匹配的字符串是空字符串:它的开始紧跟着结束。 - +唯一一个匹配的字符串是空字符串:它的开始紧跟着结束。 这个题目再一次说明了锚不是一个字符串,而是一个测试。 From 24f1ce044067bdc6d627d65a13593ae184d16173 Mon Sep 17 00:00:00 2001 From: LeviDing Date: Tue, 30 Oct 2018 11:06:54 +0800 Subject: [PATCH 4/8] Update solution.md --- 5-regular-expressions/12-regexp-anchors/1-start-end/solution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9ba96f61c6..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 @@ -3,4 +3,4 @@ 这个题目再一次说明了锚不是一个字符串,而是一个测试。 -对于空字符串 `""`,正则表达式引擎将会首先匹配模式 `^` (输入开始),匹配成功之后,会紧跟着检查模式 `$`,也匹配成功。所以空字符串是匹配 `^$` 的。 +对于空字符串 `""`,正则表达式引擎将会首先匹配模式 `^`(输入开始),匹配成功之后,会紧跟着检查模式 `$`,也匹配成功。所以空字符串是匹配 `^$` 的。 From 1124ac227ad538ad6a540e4590c0d34c699a8fb2 Mon Sep 17 00:00:00 2001 From: LeviDing Date: Tue, 30 Oct 2018 11:08:22 +0800 Subject: [PATCH 5/8] Update solution.md --- .../12-regexp-anchors/2-test-mac/solution.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 bf626d7224..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 @@ -7,14 +7,15 @@ 现在让我们看看此模式如何匹配整个文本:从 `^` 处开始,到 `$` 这里结束。通过将匹配模式包裹在 `^...$` 来完成的。 最终结果: -```js + +```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 (缺少冒号) +alert( reg.test('0132546789AB') ); // false(缺少冒号) -alert( reg.test('01:32:54:67:89') ); // false (只有5个数字,必须是6个数字) +alert( reg.test('01:32:54:67:89') ); // false(只有 5 个数字,必须是 6 个数字) -alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ 不是合法的十六进制) +alert( reg.test('01:32:54:67:89:ZZ') ) // false(ZZ 不是合法的十六进制) ``` From 7cf9cd7ac210162d9b825f715bc2fe6e79c3dafc Mon Sep 17 00:00:00 2001 From: LeviDing Date: Tue, 30 Oct 2018 11:11:42 +0800 Subject: [PATCH 6/8] Update task.md --- .../12-regexp-anchors/2-test-mac/task.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 29f86f18b3..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,21 +1,21 @@ # 检验 MAC 地址 -作为互联网接口的 [MAC 地址](https://en.wikipedia.org/wiki/MAC_address) 包括了6个以冒号 `:` 分隔的两位十六进制数。 +作为互联网接口的 [MAC 地址](https://en.wikipedia.org/wiki/MAC_address) 包括了 6 个以冒号 `:` 分隔的两位十六进制数。 举个例子:`subject:'01:32:54:67:89:AB'`。 请写一个能检查所有 MAC 地址的正则表达式。 用法: + ```js let reg = /your regexp/; alert( reg.test('01:32:54:67:89:AB') ); // true -alert( reg.test('0132546789AB') ); // false (缺少冒号) +alert( reg.test('0132546789AB') ); // false(缺少冒号) -alert( reg.test('01:32:54:67:89') ); // false (只有5个数字,必须是6个数字) +alert( reg.test('01:32:54:67:89') ); // false(只有 5 个数字,必须是 6 个数字) -alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ 不是合法的十六进制) +alert( reg.test('01:32:54:67:89:ZZ') ) // false(ZZ 不是合法的十六进制) ``` - From b0bd57cbf633261b2792223a1312614da161ea39 Mon Sep 17 00:00:00 2001 From: LeviDing Date: Tue, 30 Oct 2018 11:15:58 +0800 Subject: [PATCH 7/8] Update article.md --- 5-regular-expressions/12-regexp-anchors/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/5-regular-expressions/12-regexp-anchors/article.md b/5-regular-expressions/12-regexp-anchors/article.md index 625524a4fe..06623f9535 100644 --- a/5-regular-expressions/12-regexp-anchors/article.md +++ b/5-regular-expressions/12-regexp-anchors/article.md @@ -52,4 +52,4 @@ alert( /^#[0-9a-f]{6}$/i.test(str) ); // true 换句话说,它们不会检验到一个字符,但是它们会让正则表达式的引擎去检查判定条件(例如是否处于文本开头/结尾)。 ``` -这些锚的性质会在正则表达式有一个 `m`(多行模式)flag 时改变。我们将会在下一章讨论它。 +当正则表达式有一个 `m`(多行模式)flag 时,这些锚的性质会发生改变。我们将会在下一章讨论它。 From d405133e254d9a25e567fdfd2b906a7827e21f83 Mon Sep 17 00:00:00 2001 From: LeviDing Date: Tue, 30 Oct 2018 11:16:39 +0800 Subject: [PATCH 8/8] Update article.md --- 5-regular-expressions/13-regexp-multiline-mode/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/5-regular-expressions/13-regexp-multiline-mode/article.md b/5-regular-expressions/13-regexp-multiline-mode/article.md index 781fac7d7a..25ff611428 100644 --- a/5-regular-expressions/13-regexp-multiline-mode/article.md +++ b/5-regular-expressions/13-regexp-multiline-mode/article.md @@ -1,4 +1,4 @@ -# Flag "m"—— 多行模式 +# Flag "m" — 多行模式 通过 flag `/.../m` 可以开启多行模式。