2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7db6367417b2b2512b9a
|
2021-03-14 21:20:39 -06:00
|
|
|
|
title: 匹配出现零次或多次的字符
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:01 +08:00
|
|
|
|
forumTopicId: 301351
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: match-characters-that-occur-zero-or-more-times
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
上一次的挑战中使用了加号 `+` 来查找出现一次或多次的字符。 还有一个选项可以匹配出现零次或多次的字符。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
执行该操作的字符叫做星号,即`*`。
|
2020-08-04 15:14:01 +08:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let soccerWord = "gooooooooal!";
|
|
|
|
|
let gPhrase = "gut feeling";
|
|
|
|
|
let oPhrase = "over the moon";
|
|
|
|
|
let goRegex = /go*/;
|
2021-03-14 21:20:39 -06:00
|
|
|
|
soccerWord.match(goRegex);
|
|
|
|
|
gPhrase.match(goRegex);
|
|
|
|
|
oPhrase.match(goRegex);
|
2020-08-04 15:14:01 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
按顺序排列,三次 `match` 调用将返回值 `["goooooooo"]`,`["g"]` 和 `null`。
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
在这个挑战里,`chewieQuote` 已经被初始化为 `Aaaaaaaaaaaaaaaarrrgh!`。 创建一个变量为 `chewieRegex` 的正则表达式,使用 `*` 在 `chewieQuote` 中匹配 `A` 及其之后出现的零个或多个`a`。 你的正则表达式不需要使用修饰符,也不需要匹配引号。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
你的正则表达式 `chewieRegex` 应该使用 `*` 字符来匹配零或更多的 `a` 字符。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(/\*/.test(chewieRegex.source));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
正则表达式应当匹配 `chewieQuote` 里的 `A`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(result[0][0] === 'A');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
你的正则表达式应该与 `chewieQuote` 中的字符串 `Aaaaaaaaaaaaaaaa` 匹配。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(result[0] === 'Aaaaaaaaaaaaaaaa');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
你的正则表达式 `chewieRegex` 应该匹配 `chewieQuote` 中的 16 个字符。
|
2020-08-04 15:14:01 +08:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(result[0].length === 16);
|
2020-08-04 15:14:01 +08:00
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-06-30 20:47:19 +05:30
|
|
|
|
你的正则表达式不应该匹配字符串 `He made a fair move. Screaming about it can't help you.` 中的任何字符。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
!"He made a fair move. Screaming about it can't help you.".match(chewieRegex)
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-06-30 20:47:19 +05:30
|
|
|
|
你的正则表达式不应该匹配字符串 `Let him have it. It's not wise to upset a Wookiee.` 中的任何字符。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(
|
|
|
|
|
!"Let him have it. It's not wise to upset a Wookiee.".match(chewieRegex)
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-04 15:14:01 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --before-user-code--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
let chewieRegex = /change/; // Change this line
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
|
|
|
|
|
let result = chewieQuote.match(chewieRegex);
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
let chewieRegex = /Aa*/;
|
|
|
|
|
let result = chewieQuote.match(chewieRegex);
|
|
|
|
|
```
|