2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5d712346c441eddfaeb5bdef
|
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:01 +08:00
|
|
|
|
forumTopicId: 18181
|
2020-10-01 17:54:21 +02:00
|
|
|
|
title: 匹配所有数字
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-08-04 15:14:01 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
已经了解了常见字符串匹配模式的元字符,如字母数字。另一个常见的匹配模式是只寻找数字。
|
|
|
|
|
查找数字字符的缩写是<code>\d</code>,注意是小写的<code>d</code>。这等同于元字符<code>[0-9]</code>,它查找 0 到 9 之间任意数字的单个字符。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:01 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
使用缩写<code>\d</code>来计算电影标题中有多少个数字。书面数字("six" 而不是 6)不计算在内。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-08-04 15:14:01 +08:00
|
|
|
|
- text: 你的正则表达式应该使用缩写来匹配数字字符。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(/\\d/.test(numRegex.source));
|
2020-08-04 15:14:01 +08:00
|
|
|
|
- text: 你的正则表达式应该使用全局状态修正符。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(numRegex.global);
|
2020-08-04 15:14:01 +08:00
|
|
|
|
- text: "你的正则表达式应该在<code>'9'</code>中匹配到 1 个数字。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert("9".match(numRegex).length == 1);
|
2020-08-04 15:14:01 +08:00
|
|
|
|
- text: "你的正则表达式应该在<code>'Catch 22'</code>中匹配到 2 个数字。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert("Catch 22".match(numRegex).length == 2);
|
2020-08-04 15:14:01 +08:00
|
|
|
|
- text: "你的正则表达式应该在<code>'101 Dalmatians'</code>中匹配到 3 个数字。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert("101 Dalmatians".match(numRegex).length == 3);
|
2020-08-04 15:14:01 +08:00
|
|
|
|
- text: "你的正则表达式在<code>'One, Two, Three'</code>中应该匹配不到数字。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert("One, Two, Three".match(numRegex) == null);
|
2020-08-04 15:14:01 +08:00
|
|
|
|
- text: "你的正则表达式应该在<code>'21 Jump Street'</code>中匹配到 2 个数字。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert("21 Jump Street".match(numRegex).length == 2);
|
2020-08-04 15:14:01 +08:00
|
|
|
|
- text: '你的正则表达式应该在<code>"2001: A Space Odyssey"</code>中匹配到 4 个数字。'
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: 'assert("2001: A Space Odyssey".match(numRegex).length == 4);'
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:14:01 +08:00
|
|
|
|
let movieName = "2001: A Space Odyssey";
|
2018-10-10 18:03:03 -04:00
|
|
|
|
let numRegex = /change/; // Change this line
|
2020-08-04 15:14:01 +08:00
|
|
|
|
let result = movieName.match(numRegex).length;
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:14:01 +08:00
|
|
|
|
let movieName = "2001: A Space Odyssey";
|
|
|
|
|
let numRegex = /\d/g; // Change this line
|
|
|
|
|
let result = movieName.match(numRegex).length;
|
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-04 15:14:01 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|