--- id: 587d7dba367417b2b2512ba9 title: Positive and Negative Lookahead challengeType: 1 videoUrl: '' localeTitle: الإيجابية و السلبية 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
```yml tests: - text: يجب أن يستخدم lookaheads إيجابية. testString: 'assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null, "Your regex should use two positive lookaheads.");' - text: يجب ألا يتطابق تعبيرك العادي مع "astronaut" testString: 'assert(!pwRegex.test("astronaut"), "Your regex should not match "astronaut"");' - text: يجب ألا يتطابق تعبيرك العادي مع "airplanes" testString: 'assert(!pwRegex.test("airplanes"), "Your regex should not match "airplanes"");' - text: يجب ألا يتطابق التعبير العادي مع "banan1" testString: 'assert(!pwRegex.test("banan1"), "Your regex should not match "banan1"");' - text: يجب أن يتطابق "bana12" العادي مع "bana12" testString: 'assert(pwRegex.test("bana12"), "Your regex should match "bana12"");' - text: يجب أن يتطابق التعبير العادي مع "abc123" testString: 'assert(pwRegex.test("abc123"), "Your regex should match "abc123"");' - text: يجب ألا يتطابق التعبير العادي مع "123" testString: 'assert(!pwRegex.test("123"), "Your regex should not match "123"");' - text: يجب ألا يتطابق التعبير العادي مع "1234" testString: 'assert(!pwRegex.test("1234"), "Your regex should not match "1234"");' ```
## Challenge Seed
```js let sampleWord = "astronaut"; let pwRegex = /change/; // Change this line let result = pwRegex.test(sampleWord); ```
## Solution
```js // solution required ```