2.9 KiB
2.9 KiB
id, title, challengeType, forumTopicId, localeTitle
id | title | challengeType | forumTopicId | localeTitle |
---|---|---|---|---|
587d7dbb367417b2b2512bab | Use Capture Groups to Search and Replace | 1 | 301368 | Использование групп захвата для поиска и замены |
Description
.replace()
в строке. Входы для .replace()
- это сначала шаблон регулярного выражения, который вы хотите найти. Второй параметр - это строка, которая заменит совпадение или функцию, чтобы что-то сделать. пусть wrongText = «Небо серебристое»;Вы также можете получить доступ к группам захвата в строке замены знаками доллара (
пусть сереброRegex = / серебро /;
wrongText.replace (silverRegex, «синий»);
// Возвращает «Небо синее».
$
). «Кодовый лагерь» .replace (/ (\ w +) \ s (\ w +) /, '$ 2 $ 1');
// Возвращает «Код лагеря»
Instructions
"good"
. Затем обновите переменную replaceText
чтобы заменить "good"
на "okey-dokey"
.
Tests
tests:
- text: You should use <code>.replace()</code> to search and replace.
testString: assert(code.match(/\.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");
- text: You should not change the last line.
testString: assert(code.match(/result\s*=\s*huhText\.replace\(.*?\)/));
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
let huhText = "This sandwich is good.";
let fixRegex = /good/g; // Change this line
let replaceText = "okey-dokey"; // Change this line
let result = huhText.replace(fixRegex, replaceText);