2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7db5367417b2b2512b97
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:01 +08:00
|
|
|
forumTopicId: 301356
|
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>-</code>)匹配字符范围并不仅限于字母。它还可以匹配一系列数字。
|
|
|
|
例如,<code>/[0-5]/</code>匹配<code>0</code>和<code>5</code>之间的任意数字,包含<code>0</code>和<code>5</code>。
|
|
|
|
此外,还可以在单个字符集中组合一系列字母和数字。
|
|
|
|
|
|
|
|
```js
|
|
|
|
let jennyStr = "Jenny8675309";
|
|
|
|
let myRegex = /[a-z0-9]/ig;
|
|
|
|
// matches all letters and numbers in jennyStr
|
|
|
|
jennyStr.match(myRegex);
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:01 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
创建一个正则表达式,使其可以匹配<code>h</code>和<code>s</code>之间的一系列字母,以及<code>2</code>和<code>6</code>之间的一系列数字。请记得在正则表达式中包含恰当的标志。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: 你的正则表达式<code>myRegex</code>应该匹配 17 项。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(result.length == 17);
|
2018-10-10 18:03:03 -04:00
|
|
|
- text: 你的正则表达式<code>myRegex</code>应该使用全局标志。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(myRegex.flags.match(/g/).length == 1);
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: 你的正则表达式<code>myRegex</code>应该使用忽略大小写的标志。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(myRegex.flags.match(/i/).length == 1);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
let quoteSample = "Blueberry 3.141592653s are delicious.";
|
|
|
|
let myRegex = /change/; // Change this line
|
|
|
|
let result = myRegex; // Change this line
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:14:01 +08:00
|
|
|
let quoteSample = "Blueberry 3.141592653s are delicious.";
|
|
|
|
let myRegex = /[h-s2-6]/gi; // Change this line
|
|
|
|
let result = quoteSample.match(myRegex); // Change this line
|
|
|
|
|
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>
|