freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-an.../regular-expressions/use-capture-groups-to-searc...

2.9 KiB

id title challengeType videoUrl localeTitle
587d7dbb367417b2b2512bab Use Capture Groups to Search and Replace 1 استخدم مجموعات الالتقاط للبحث واستبدال

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

tests:
  - text: يجب عليك استخدام <code>.replace()</code> للبحث <code>.replace()</code> .
    testString: 'assert(code.match(/\.replace\(.*\)/), "You should use <code>.replace()</code> to search and replace.");'
  - text: يجب أن يتغير تعبيرك المعتاد <code>&quot;This sandwich is good.&quot;</code> إلى <code>&quot;This sandwich is okey-dokey.&quot;</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: يجب عدم تغيير السطر الأخير.
    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