* fix(curriculum): tests quotes * fix(curriculum): fill seed-teardown * fix(curriculum): fix tests and remove unneeded seed-teardown
2.2 KiB
2.2 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d7dbb367417b2b2512bab | Use Capture Groups to Search and Replace | 1 |
Description
.replace()
on a string. The inputs for .replace()
is first the regex pattern you want to search for. The second parameter is the string to replace the match or a function to do something.
let wrongText = "The sky is silver.";You can also access capture groups in the replacement string with dollar signs (
let silverRegex = /silver/;
wrongText.replace(silverRegex, "blue");
// Returns "The sky is blue."
$
).
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
// Returns "Camp Code"
Instructions
"good"
. Then update the replaceText
variable to replace "good"
with "okey-dokey"
.
Tests
tests:
- text: You should use <code>.replace()</code> to search and replace.
testString: assert(code.match(/\.replace\(.*\)/), 'You should use <code>.replace()</code> to search and replace.');
- text: Your regex should change <code>"This sandwich is good."</code> to <code>"This sandwich is okey-dokey."</code>
testString: assert(result == "This sandwich is okey-dokey." && replaceText === "okey-dokey", 'Your regex should change <code>"This sandwich is good."</code> to <code>"This sandwich is okey-dokey."</code>');
- text: You should not change the last line.
testString: assert(code.match(/result\s*=\s*huhText\.replace\(.*?\)/), 'You should not change the last line.');
Challenge Seed
let huhText = "This sandwich is good.";
let fixRegex = /change/; // Change this line
let replaceText = ""; // Change this line
let result = huhText.replace(fixRegex, replaceText);
Solution
// solution required