.replace()搜索和替换字符串中的文本。 .replace()的输入首先是您要搜索的正则表达式模式。第二个参数是用于替换匹配的字符串或用于执行某些操作的函数。 let wrongText =“天空是银色的。”;您还可以使用美元符号(
让silverRegex = / silver /;
wrongText.replace(silverRegex,“blue”);
//返回“天空是蓝色的”。
$ )访问替换字符串中的捕获组。 “Code Camp”.replace(/(\ w +)\ s(\ w +)/,'$ 2 $ 1');
//返回“营地代码”
"good" 。然后更新replaceText变量,将"good"替换为"okey-dokey" 。 .replace()来搜索和替换。
testString: 'assert(code.match(/\.replace\(.*\)/), "You should use .replace() to search and replace.");'
- text: 你的正则表达式应该改变"This sandwich is good." "This sandwich is okey-dokey."
testString: 'assert(result == "This sandwich is okey-dokey." && replaceText === "okey-dokey", "Your regex should change "This sandwich is good." to "This sandwich is okey-dokey."");'
- text: 你不应该改变最后一行。
testString: 'assert(code.match(/result\s*=\s*huhText\.replace\(.*?\)/), "You should not change the last line.");'
```