2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7db9367417b2b2512ba5
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:01 +08:00
|
|
|
forumTopicId: 301367
|
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>*</code>查找零个或多个字符。这些都很方便,但有时需要匹配一定范围的匹配模式。
|
|
|
|
可以使用<code>数量说明符</code>指定匹配模式的上下限。数量说明符与花括号(<code>{</code>和<code>}</code>)一起使用。可以在花括号之间放两个数字,这两个数字代表匹配模式的上限和下限。
|
|
|
|
例如,要在字符串<code>"ah"</code>中匹配仅出现<code>3</code>到<code>5</code>次的字母<code>a</code>,正则表达式应为<code>/a{3,5}h/</code>。
|
|
|
|
|
|
|
|
```js
|
|
|
|
let A4 = "aaaah";
|
|
|
|
let A2 = "aah";
|
|
|
|
let multipleA = /a{3,5}h/;
|
|
|
|
multipleA.test(A4); // Returns true
|
|
|
|
multipleA.test(A2); // Returns false
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:01 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
修改正则表达式<code>ohRegex</code>以匹配在<code>"Oh no"</code>中仅出现<code>3</code>到<code>6</code>次的字母<code>h</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: 你的正则表达式应该使用花括号。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(ohRegex.source.match(/{.*?}/).length > 0);
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式不应该匹配<code>'Ohh no'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(!ohRegex.test("Ohh no"));
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式应该匹配<code>'Ohhh no'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert("Ohhh no".match(ohRegex)[0].length === 7);
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: 正则表达式应该匹配 <code>"Ohhhh no"</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert("Ohhhh no".match(ohRegex)[0].length === 8);
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式应该匹配<code>'Ohhhhh no'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert("Ohhhhh no".match(ohRegex)[0].length === 9);
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式应该匹配<code>'Ohhhhhh no'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert("Ohhhhhh no".match(ohRegex)[0].length === 10);
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式不应该匹配<code>'Ohhhhhhh no'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(!ohRegex.test("Ohhhhhhh no"));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
let ohStr = "Ohhh no";
|
|
|
|
let ohRegex = /change/; // Change this line
|
|
|
|
let result = ohRegex.test(ohStr);
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:14:01 +08:00
|
|
|
let ohStr = "Ohhh no";
|
|
|
|
let ohRegex = /Oh{3,6} no/; // Change this line
|
|
|
|
let result = ohRegex.test(ohStr);
|
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>
|