--- id: 587d7dbb367417b2b2512bab title: Use Capture Groups to Search and Replace challengeType: 1 videoUrl: '' localeTitle: استخدم مجموعات الالتقاط للبحث واستبدال --- ## Description
البحث مفيد. ومع ذلك ، يمكنك جعل البحث أكثر قوة عندما يتغير (أو يحل محل) النص الذي تطابقه. يمكنك البحث واستبدال النص في سلسلة باستخدام .replace() في سلسلة. تعتبر مدخلات .replace() هي أولاً نمط regex الذي تريد البحث عنه. المعلمة الثانية هي السلسلة التي ستحل محل المطابقة أو الوظيفة لفعل شيء ما.
let wrongText = "إن السماء فضية."؛
let silverRegex = / silver /؛
wrongText.replace (silverRegex، "blue")؛
// Returns "السماء زرقاء."
يمكنك أيضًا الوصول إلى مجموعات الالتقاط في سلسلة الاستبدال باستخدام علامة الدولار ( $ ).
"Code Code" .replace (/ (\ w +) \ s (\ w +) /، '$ 2 $ 1')؛
// إرجاع "رمز المعسكر"
## Instructions
اكتب تعبيرًا منطقيًا حتى يبحث عن السلسلة "good" . ثم قم بتحديث متغير replaceText لاستبدال "good" بـ "okey-dokey" .
## Tests
```yml tests: - text: يجب عليك استخدام .replace() للبحث .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.");' ```
## Challenge Seed
```js let huhText = "This sandwich is good."; let fixRegex = /change/; // Change this line let replaceText = ""; // Change this line let result = huhText.replace(fixRegex, replaceText); ```
## Solution
```js // solution required ```