freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-an.../regular-expressions/positive-and-negative-looka...

4.5 KiB

id title challengeType videoUrl localeTitle
587d7dba367417b2b2512ba9 Positive and Negative Lookahead 1 الإيجابية و السلبية Lookahead

Description

Lookaheads هي الأنماط التي تخبر جافا سكريبت أن ننظر إلى الأمام في السلسلة الخاصة بك للتحقق من وجود أنماط أخرى على طول. يمكن أن يكون ذلك مفيدًا عندما تريد البحث عن أنماط متعددة عبر نفس السلسلة. هناك نوعان من lookaheads : positive lookahead و negative lookahead . سوف ننظر positive lookahead للتأكد من وجود عنصر في نمط البحث ، ولكن لن تتطابق في الواقع. يتم استخدام lookahead إيجابية كما (?=...) حيث ... هو الجزء المطلوب غير متطابق. من ناحية أخرى ، سوف ننظر negative lookahead للتأكد من عدم وجود عنصر في نمط البحث. يتم استخدام lookahead سلبي كما (?!...) حيث ... هو النمط الذي لا تريد أن تكون هناك. يتم إرجاع بقية النمط إذا كان جزء lookahead السلبي غير موجود. Lookaheads مربكة بعض الشيء ولكن بعض الأمثلة سوف تساعد.
ترك quit = "qu"؛
let noquit = "qt"؛
let quRegex = / q (؟ = u) /؛
اترك qRegex = / q (؟! u) /؛
quit.match (quRegex)؛ // Returns ["q"]
noquit.match (qRegex)؛ // Returns ["q"]
استخدام أكثر عملية من lookaheads هو التحقق من اثنين أو أكثر من الأنماط في سلسلة واحدة. هنا مدقق كلمات مرور بسيط (بسذاجة) يبحث عن 3 إلى 6 أحرف ورقم واحد على الأقل:
السماح بكلمة المرور = "abc123" ؛
let checkPass = / (؟ = \ w {3،6}) (؟ = \ D * \ d) /؛
checkPass.test (كلمة السر)؛ // يعود صحيح

Instructions

استخدم lookaheads في pwRegex لمطابقة كلمات المرور التي يزيد طولها عن 5 أحرف ولها رقمين متتاليين.

Tests

tests:
  - text: يجب أن يستخدم <code>lookaheads</code> إيجابية.
    testString: 'assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null, "Your regex should use two positive <code>lookaheads</code>.");'
  - text: يجب ألا يتطابق تعبيرك العادي مع <code>&quot;astronaut&quot;</code>
    testString: 'assert(!pwRegex.test("astronaut"), "Your regex should not match <code>"astronaut"</code>");'
  - text: يجب ألا يتطابق تعبيرك العادي مع <code>&quot;airplanes&quot;</code>
    testString: 'assert(!pwRegex.test("airplanes"), "Your regex should not match <code>"airplanes"</code>");'
  - text: يجب ألا يتطابق التعبير العادي مع <code>&quot;banan1&quot;</code>
    testString: 'assert(!pwRegex.test("banan1"), "Your regex should not match <code>"banan1"</code>");'
  - text: يجب أن يتطابق <code>&quot;bana12&quot;</code> العادي مع <code>&quot;bana12&quot;</code>
    testString: 'assert(pwRegex.test("bana12"), "Your regex should match <code>"bana12"</code>");'
  - text: يجب أن يتطابق التعبير العادي مع <code>&quot;abc123&quot;</code>
    testString: 'assert(pwRegex.test("abc123"), "Your regex should match <code>"abc123"</code>");'
  - text: يجب ألا يتطابق التعبير العادي مع <code>&quot;123&quot;</code>
    testString: 'assert(!pwRegex.test("123"), "Your regex should not match <code>"123"</code>");'
  - text: يجب ألا يتطابق التعبير العادي مع <code>&quot;1234&quot;</code>
    testString: 'assert(!pwRegex.test("1234"), "Your regex should not match <code>"1234"</code>");'

Challenge Seed

let sampleWord = "astronaut";
let pwRegex = /change/; // Change this line
let result = pwRegex.test(sampleWord);

Solution

// solution required