(?=\D*\d) is looking for a non-number to precede a number. If '12Three' was passed in to test, it would not match though it matches the stated criteria of 5 character minimum length and two consecutive numbers.
3.5 KiB
3.5 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d7dba367417b2b2512ba9 | Positive and Negative Lookahead | 1 |
Description
Lookaheads
are patterns that tell JavaScript to look-ahead in your string to check for patterns further along. This can be useful when you want to search for multiple patterns over the same string.
There are two kinds of lookaheads
: positive lookahead
and negative lookahead
.
A positive lookahead
will look to make sure the element in the search pattern is there, but won't actually match it. A positive lookahead is used as (?=...)
where the ...
is the required part that is not matched.
On the other hand, a negative lookahead
will look to make sure the element in the search pattern is not there. A negative lookahead is used as (?!...)
where the ...
is the pattern that you do not want to be there. The rest of the pattern is returned if the negative lookahead part is not present.
Lookaheads are a bit confusing but some examples will help.
let quit = "qu";A more practical use of
let noquit = "qt";
let quRegex= /q(?=u)/;
let qRegex = /q(?!u)/;
quit.match(quRegex); // Returns ["q"]
noquit.match(qRegex); // Returns ["q"]
lookaheads
is to check two or more patterns in one string. Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:
let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password); // Returns true
Instructions
lookaheads
in the pwRegex
to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits.
Tests
tests:
- text: Your regex should use two positive <code>lookaheads</code>.
testString: assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null, 'Your regex should use two positive <code>lookaheads</code>.');
- text: Your regex should not match <code>"astronaut"</code>
testString: assert(!pwRegex.test("astronaut"), 'Your regex should not match <code>"astronaut"</code>');
- text: Your regex should not match <code>"airplanes"</code>
testString: assert(!pwRegex.test("airplanes"), 'Your regex should not match <code>"airplanes"</code>');
- text: Your regex should not match <code>"banan1"</code>
testString: assert(!pwRegex.test("banan1"), 'Your regex should not match <code>"banan1"</code>');
- text: Your regex should match <code>"bana12"</code>
testString: assert(pwRegex.test("bana12"), 'Your regex should match <code>"bana12"</code>');
- text: Your regex should match <code>"abc123"</code>
testString: assert(pwRegex.test("abc123"), 'Your regex should match <code>"abc123"</code>');
- text: Your regex should not match <code>"123"</code>
testString: assert(!pwRegex.test("123"), 'Your regex should not match <code>"123"</code>');
- text: Your regex should not match <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
var pwRegex = /(?=\w{5})(?=\D*\d{2})/;