2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 587d7db9367417b2b2512ba5
|
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: 301367
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: specify-upper-and-lower-number-of-matches
|
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
|
|
|
回想一下,使用加号 `+` 查找一个或多个字符,使用星号 `*` 查找零个或多个字符。 这些都很方便,但有时需要匹配一定范围的匹配模式。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
可以使用数量说明符(<dfn>quantity specifiers</dfn>)指定匹配模式的上下限。 数量说明符与花括号(`{` 和 `}`)一起使用。 可以在花括号之间放两个数字,这两个数字代表匹配模式的上限和下限。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
例如,要匹配出现 `3` 到 `5` 次字母 `a` 的在字符串 `ah`,正则表达式应为`/a{3,5}h/`。
|
2020-08-04 07:14:01 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
let A4 = "aaaah";
|
|
|
|
let A2 = "aah";
|
|
|
|
let multipleA = /a{3,5}h/;
|
2021-03-15 03:20:39 +00:00
|
|
|
multipleA.test(A4);
|
|
|
|
multipleA.test(A2);
|
2020-08-04 07:14:01 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
第一次 `test` 调用将返回 `true`,而第二次调用将返回 `false`。
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
修改正则表达式 `ohRegex` 以匹配出现 `3` 到 `6` 次字母 `h` 的字符串 `Oh no`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该使用花括号。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(ohRegex.source.match(/{.*?}/).length > 0);
|
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式不应匹配字符串 `Ohh no`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(!ohRegex.test('Ohh no'));
|
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该匹配字符串 `Ohhh no`
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert('Ohhh no'.match(ohRegex)[0].length === 7);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该匹配字符串 `Ohhhh no`
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert('Ohhhh no'.match(ohRegex)[0].length === 8);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该匹配字符串 `Ohhhhh no`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert('Ohhhhh no'.match(ohRegex)[0].length === 9);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该匹配字符串 `Ohhhhhh no`
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert('Ohhhhhh no'.match(ohRegex)[0].length === 10);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该匹配字符串 `Ohhhhhhh no`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(!ohRegex.test('Ohhhhhhh no'));
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-08-04 07:14:01 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
let ohStr = "Ohhh no";
|
|
|
|
let ohRegex = /change/; // Change this line
|
|
|
|
let result = ohRegex.test(ohStr);
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
let ohStr = "Ohhh no";
|
|
|
|
let ohRegex = /Oh{3,6} no/; // Change this line
|
|
|
|
let result = ohRegex.test(ohStr);
|
|
|
|
```
|