2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d7db5367417b2b2512b95
|
2021-03-15 03:20:39 +00:00
|
|
|
|
title: 将单个字符与多种可能性匹配
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 1
|
2020-08-04 07:14:01 +00:00
|
|
|
|
forumTopicId: 301357
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: match-single-character-with-multiple-possibilities
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
已经了解了文字匹配模式(`/literal/`)和通配符(`/./`)。 这是正则表达式的两种极端情况,一种是精确匹配,而另一种则是匹配所有。 在这两种极端情况之间有一个平衡选项。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
可以使用字符集 (<dfn>character classes</dfn>)更灵活的匹配字符。 可以把字符集放在方括号(`[` 和 `]`)之间来定义一组需要匹配的字符串。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
例如,如果想要匹配 `bag`、`big` 和 `bug`,但是不想匹配 `bog`。 可以创建正则表达式 `/b[aiu]g/` 来执行此操作。 `[aiu]` 是只匹配字符 `a`、`i` 或者 `u` 的字符集。
|
2020-08-04 07:14:01 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let bigStr = "big";
|
|
|
|
|
let bagStr = "bag";
|
|
|
|
|
let bugStr = "bug";
|
|
|
|
|
let bogStr = "bog";
|
|
|
|
|
let bgRegex = /b[aiu]g/;
|
2021-03-15 03:20:39 +00:00
|
|
|
|
bigStr.match(bgRegex);
|
|
|
|
|
bagStr.match(bgRegex);
|
|
|
|
|
bugStr.match(bgRegex);
|
|
|
|
|
bogStr.match(bgRegex);
|
2020-08-04 07:14:01 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
按顺序排列,四次 `match` 调用将返回值 `["big"]`、`["bag"]`、`["bug"]` 和 `null`。
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
使用元音字符集(`a`、`e`、`i`、`o`、`u`)在正则表达式 `vowelRegex` 中匹配到字符串 `quoteSample` 中的所有元音。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
**注意:**一定要同时匹配大小写元音。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你应该匹配到所有25个元音。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(result.length == 25);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你的正则表达式 `vowelRegex` 应该使用字符集。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(/\[.*\]/.test(vowelRegex.source));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你的正则表达式 `vowelRegex` 应该使用全局标志。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(vowelRegex.flags.match(/g/).length == 1);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你的正则表达式 `vowelRegex` 应该使用忽略大小写标志。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(vowelRegex.flags.match(/i/).length == 1);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-04 07:14:01 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你的正则表达式不应该匹配任何辅音。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(!/[b-df-hj-np-tv-z]/gi.test(result.join()));
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
|
|
|
|
|
let vowelRegex = /change/; // Change this line
|
|
|
|
|
let result = vowelRegex; // Change this line
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
|
|
|
|
|
let vowelRegex = /[aeiou]/gi; // Change this line
|
|
|
|
|
let result = quoteSample.match(vowelRegex); // Change this line
|
|
|
|
|
```
|