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

3.6 KiB

id title challengeType forumTopicId dashedName
587d7dba367417b2b2512ba9 نظرة إيجابية وسلبية (Positive and Negative Lookahead) 1 301360 positive-and-negative-lookahead

--description--

Lookahead هي الأنماط التي تخبر جافا سكريبت بالبحث في الـ string الخاص بك للتحقق من الأنماط بداخله. يمكن أن يكون هذا مفيداً عندما تريد البحث عن أنماط متعددة في نفس الـ string.

هناك نوعان من الـ lookaheads: الـ: positive lookahead و negative lookahead.

Positive lookahead تنظر للتأكد من أن العنصر في نمط البحث متواجد، ولكن لن نطابقه في الواقع. يتم استخدام الـ positive lookahead كـ (?=...) حيث ... هو الجزء المطلوب غير المتطابق.

ومن ناحية أخرى، فإن الـ negative lookahead ستنظر للتأكد من عدم وجود عنصر في نمط البحث. يتم استخدام الـ negative lookahead كـ (?!...) حيث ... هو النمط الذي لا تريد ان يكون متواجد. ويعاد ما تبقى من النمط إذا لم يكن جزء الـ negative lookahead موجودا.

Lookaheads مربكة بعض الشيء ولكن بعض الأمثلة سوف تساعد.

let quit = "qu";
let noquit = "qt";
let quRegex= /q(?=u)/;
let qRegex = /q(?!u)/;
quit.match(quRegex);
noquit.match(qRegex);

هذان الاستدعائان لـ match سيرجعان ["q"].

وهناك استخدام عملي أكثر للـ lookaheads يتمثل في فحص نمطين أو أكثر في string واحد. إليك مدقق كلمة مرور بسيط يبحث عن ما بين 3 و 6 أحرف ورقم واحد على الأقل:

let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password);

--instructions--

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

--hints--

يجب أن يستخدم الـ regex الخاص بك اثنين lookaheads يكونان positive.

assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);

يجب أن لا يطابق الـ regex الخاص بك astronaut

pwRegex.lastIndex = 0;
assert(!pwRegex.test('astronaut'));

يجب أن لا يطابق الـ regex الخاص بك banan1

pwRegex.lastIndex = 0;
assert(!pwRegex.test('banan1'));

يجب أن يطابق الـ regex الخاص بك bana12

pwRegex.lastIndex = 0;
assert(pwRegex.test('bana12'));

يجب أن يطابق الـ regex الخاص بك abc123

pwRegex.lastIndex = 0;
assert(pwRegex.test('abc123'));

يجب أن لا يطابق الـ regex الخاص بك 12345

pwRegex.lastIndex = 0;
assert(!pwRegex.test('12345'));

يجب أن يطابق الـ regex الخاص بك 8pass99

pwRegex.lastIndex = 0;
assert(pwRegex.test('8pass99'));

يجب أن لا يطابق الـ regex الخاص بك 1a2bcde

pwRegex.lastIndex = 0;
assert(!pwRegex.test('1a2bcde'));

يجب أن يطابق الـ regex الخاص بك astr1on11aut

pwRegex.lastIndex = 0;
assert(pwRegex.test('astr1on11aut'));

--seed--

--seed-contents--

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

--solutions--

let pwRegex =  /(?=\w{6})(?=\w*\d{2})/;