Skip to content

9-regular-expressions tiny update #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 1-js/05-data-types/09-keys-values-entries/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

- `Map`
- `Set`
- `Array`(除了 `arr.values()`)
- `Array`

纯对象也支持类似的方法,但是语法上有一些不同

Expand Down
2 changes: 1 addition & 1 deletion 1-js/05-data-types/11-date/6-get-seconds-today/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ importance: 5
getSecondsToday() == 36000 // (3600 * 10)
```

该函数应该在任意一天都能正确运行。那意味着,不应该有一个「今天」这个参数不能有意外值
该函数应该在任意一天都能正确运行。那意味着,它不应具有「今天」的硬编码值
2 changes: 1 addition & 1 deletion 9-regular-expressions/05-regexp-multiline-mode/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ alert( str.match(/\w+$/gim) ); // Winnie,Piglet,Eeyore

## 锚符 ^$ 对比 \n

要寻找新的一行的话,我们不仅可以使用锚符 `^` 和 `$`,也可以使用批匹配符 `\n`。
要寻找新的一行的话,我们不仅可以使用锚符 `^` 和 `$`,也可以使用换行符 `\n`。

它和锚符 `^` 和 `$` 的第一个不同点是它不像锚符那样,它会“消耗”掉 `\n` 并且将其(`\n`)加入到匹配结果中。

Expand Down
6 changes: 3 additions & 3 deletions 9-regular-expressions/07-regexp-escaping/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ alert( "Chapter 5.1".match(regexp) ); // 5.1

## Summary

- To search for special characters `pattern:[ \ ^ $ . | ? * + ( )` literally, we need to prepend them with a backslash `\` ("escape them").
- We also need to escape `/` if we're inside `pattern:/.../` (but not inside `new RegExp`).
- When passing a string `new RegExp`, we need to double backslashes `\\`, cause string quotes consume one of them.
- 要在字面(意义)上搜索特殊字符 `pattern:[ \ ^ $ . | ? * + ( )`,我们需要在它们前面加上反斜杠 `\`("转义它们")。
- 如果我们在 `pattern:/.../` 内部(但不在 `new RegExp` 内部),还需要转义 `/`。
- 传递一个字符串(参数)给 `new RegExp` 时,我们需要双倍反斜杠 `\\`,因为字符串引号会消费其中的一个。
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ alert( "Voila".match(/V[oi]la/) ); // null,并没有匹配上

- `pattern:V`,
- 然后匹配其中的**一个字符** `pattern:[oi]`,
- 然后匹配 `pattern:V`,
- 然后匹配 `pattern:la`,

所以可以匹配上 `match:Vola` 或者 `match:Vila`。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ let str = "color:#121212; background-color:#AA00ef bad-colors:f#fddee #fd2"
alert( str.match(reg) ); // #121212,#AA00ef
```

问题是匹配到颜色值过长
问题是其从更长的序列中匹配了颜色值

```js run
alert( "#12345678".match( /#[a-f0-9]{6}/gi ) ) // #12345678
alert( "#12345678".match( /#[a-f0-9]{6}/gi ) ) // #123456
```

为了解决这个问题,我们可以在末尾加上 `pattern:\b`:
Expand Down
2 changes: 1 addition & 1 deletion 9-regular-expressions/10-regexp-greedy-and-lazy/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ alert( str.match(reg) ); // witch, broom

![](witch_lazy6.svg)

在这个例子中,我们看到了懒惰模式 `pattern:+?` 是怎样工作的。量词 `pattern:+?` 和 `pattern:??` 也有类似的效果 —— 只有在模式的剩余部分无法在给定位置匹配时,正则表达式引擎才会增加重复次数。
在这个例子中,我们看到了懒惰模式 `pattern:+?` 是怎样工作的。量词 `pattern:*?` 和 `pattern:??` 也有类似的效果 —— 只有在模式的剩余部分无法在给定位置匹配时,正则表达式引擎才会增加重复次数。

**懒惰模式只能够通过带 `?` 的量词启用**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ let reg = /#([a-f0-9]{3}){1,2}\b/gi;

let str = "color: #3f3; background-color: #AA00ef; and: #abcd";

alert( str.match(reg) ); // #3f3 #AA0ef
alert( str.match(reg) ); // #3f3 #AA00ef
```