4.5 KiB
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>"astronaut"</code>
testString: 'assert(!pwRegex.test("astronaut"), "Your regex should not match <code>"astronaut"</code>");'
- text: يجب ألا يتطابق تعبيرك العادي مع <code>"airplanes"</code>
testString: 'assert(!pwRegex.test("airplanes"), "Your regex should not match <code>"airplanes"</code>");'
- text: يجب ألا يتطابق التعبير العادي مع <code>"banan1"</code>
testString: 'assert(!pwRegex.test("banan1"), "Your regex should not match <code>"banan1"</code>");'
- text: يجب أن يتطابق <code>"bana12"</code> العادي مع <code>"bana12"</code>
testString: 'assert(pwRegex.test("bana12"), "Your regex should match <code>"bana12"</code>");'
- text: يجب أن يتطابق التعبير العادي مع <code>"abc123"</code>
testString: 'assert(pwRegex.test("abc123"), "Your regex should match <code>"abc123"</code>");'
- text: يجب ألا يتطابق التعبير العادي مع <code>"123"</code>
testString: 'assert(!pwRegex.test("123"), "Your regex should not match <code>"123"</code>");'
- text: يجب ألا يتطابق التعبير العادي مع <code>"1234"</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