2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d7dbb367417b2b2512baa
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 使用捕获组重用模式
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 1
|
2020-08-04 07:14:01 +00:00
|
|
|
|
forumTopicId: 301364
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: reuse-patterns-using-capture-groups
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-01-21 18:07:18 +00:00
|
|
|
|
一些你所搜寻的匹配模式会在字符串中出现多次, 手动重复该正则表达式显得不够简洁。 当字符串中出现多个重复子字符串时,有一种更好的方式来编写模式。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-01-21 18:07:18 +00:00
|
|
|
|
可以使用 <dfn>捕获组</dfn> 搜寻重复的子字符串。 括号 `(` 和 `)` 可以用来匹配重复的子字符串。 把需要重复匹配的模式放在括号中即可。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-01-21 18:07:18 +00:00
|
|
|
|
要指定重复字符串将出现的位置,可以使用反斜杠(<code>\\</code>)后接一个数字。 这个数字从 1 开始,随着你使用的每个捕获组的增加而增加。 这里有一个示例,`\1`可以匹配第一个组。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-01-21 18:07:18 +00:00
|
|
|
|
下面的示例展示的是匹配被空格隔开的两个相同单词:
|
2020-08-04 07:14:01 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let repeatStr = "regex regex";
|
|
|
|
|
let repeatRegex = /(\w+)\s\1/;
|
|
|
|
|
repeatRegex.test(repeatStr); // Returns true
|
|
|
|
|
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-21 18:07:18 +00:00
|
|
|
|
在字符串上调用 `.match()` 方法将返回一个数组,其中包含它最终匹配到的字符串及其捕获组。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
Use capture groups in `reRegex` to match a string that consists of only the same number repeated exactly three times separated by single spaces.
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-01-21 18:07:18 +00:00
|
|
|
|
你的正则表达式应该使用数字的简写字符类。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(reRegex.source.match(/\\d/));
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-21 18:07:18 +00:00
|
|
|
|
你的正则表达式应该使用两次捕获组。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
|
|
|
|
|
```
|
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
Your regex should match `"42 42 42"`.
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(reRegex.test('42 42 42'));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
Your regex should match `"100 100 100"`.
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(reRegex.test('100 100 100'));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
Your regex should not match `"42 42 42 42"`.
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.equal('42 42 42 42'.match(reRegex.source), null);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
Your regex should not match `"42 42"`.
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.equal('42 42'.match(reRegex.source), null);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
Your regex should not match `"101 102 103"`.
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(!reRegex.test('101 102 103'));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-04 07:14:01 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
Your regex should not match `"1 2 3"`.
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(!reRegex.test('1 2 3'));
|
|
|
|
|
```
|
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
Your regex should match `"10 10 10"`.
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(reRegex.test('10 10 10'));
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let repeatNum = "42 42 42";
|
|
|
|
|
let reRegex = /change/; // Change this line
|
|
|
|
|
let result = reRegex.test(repeatNum);
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
let repeatNum = "42 42 42";
|
|
|
|
|
let reRegex = /^(\d+)\s\1\s\1$/;
|
|
|
|
|
let result = reRegex.test(repeatNum);
|
|
|
|
|
```
|