2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d7dbb367417b2b2512bab
|
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: 301368
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: use-capture-groups-to-search-and-replace
|
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
|
|
|
|
可以在字符串上使用 `.replace()` 方法来搜索并替换字符串中的文本。 `.replace()` 的输入首先是想要搜索的正则表达式匹配模式。 第二个参数是用于替换匹配的字符串或用于执行某些操作的函数。
|
2020-08-04 07:14:01 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let wrongText = "The sky is silver.";
|
|
|
|
|
let silverRegex = /silver/;
|
|
|
|
|
wrongText.replace(silverRegex, "blue");
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`replace` 调用将返回字符串 `The sky is blue.`。
|
|
|
|
|
|
|
|
|
|
你还可以使用美元符号(`$`)访问替换字符串中的捕获组。
|
2020-08-04 07:14:01 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
调用 `replace` 将返回字符串 `Camp Code`。
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
使用三个捕获组编写一个正则表达式 `fixRegex`,这三个捕获组将搜索字符串 `one two three` 中的每个单词。 然后更新 `replaceText` 变量,以字符串 `three two one` 替换 `one two three`,并将结果分配给 `result` 变量。 确保使用美元符号(`$`)语法在替换字符串中使用捕获组。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你应该使用 `.replace()` 搜索并替换。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/\.replace\(.*\)/));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你的正则表达式应该将字符串 `one two three` 更改为字符串 `three two one`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
assert(result === 'three two one');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
你不应该改变最后一行。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
assert(code.match(/result\s*=\s*str\.replace\(.*?\)/));
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`fixRegex` 应该至少使用三个抓取组。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(new RegExp(fixRegex.source + '|').exec('').length - 1 >= 3);
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`replaceText` 应该使用括号化的子匹配字符串(例如:nth 括号化的子匹配字符串, $n, 对应于第 n 个捕获组)。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
{
|
|
|
|
|
const re = /(\$\d{1,2})+(?:[\D]|\b)/g;
|
|
|
|
|
assert(replaceText.match(re).length >= 3);
|
|
|
|
|
}
|
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 str = "one two three";
|
|
|
|
|
let fixRegex = /change/; // Change this line
|
|
|
|
|
let replaceText = ""; // Change this line
|
|
|
|
|
let result = str.replace(fixRegex, replaceText);
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
let str = "one two three";
|
|
|
|
|
let fixRegex = /(\w+) (\w+) (\w+)/g; // Change this line
|
|
|
|
|
let replaceText = "$3 $2 $1"; // Change this line
|
|
|
|
|
let result = str.replace(fixRegex, replaceText);
|
|
|
|
|
```
|