2018-10-10 22:03:03 +00:00
---
id: 587d7dba367417b2b2512ba9
2021-02-06 04:42:36 +00:00
title: Positive and Negative Lookahead
2018-10-10 22:03:03 +00:00
challengeType: 1
2020-08-04 07:14:01 +00:00
forumTopicId: 301360
2021-01-13 02:31:00 +00:00
dashedName: positive-and-negative-lookahead
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
< dfn > Lookaheads< / dfn > 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.
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
There are two kinds of lookaheads: < dfn > positive lookahead< / dfn > and < dfn > negative lookahead< / dfn > .
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
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.
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
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.
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
Lookaheads are a bit confusing but some examples will help.
2020-08-04 07:14:01 +00:00
```js
let quit = "qu";
let noquit = "qt";
let quRegex= /q(?=u)/;
let qRegex = /q(?!u)/;
quit.match(quRegex); // Returns ["q"]
noquit.match(qRegex); // Returns ["q"]
```
2021-02-06 04:42:36 +00:00
A more practical use of 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:
2020-08-04 07:14:01 +00:00
```js
let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password); // Returns true
```
2020-12-16 07:37:30 +00:00
# --instructions--
2021-02-06 04:42:36 +00:00
Use lookaheads in the `pwRegex` to match passwords that are greater than 5 characters long, and have two consecutive digits.
2020-08-04 07:14:01 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2020-08-04 07:14:01 +00:00
2021-02-06 04:42:36 +00:00
Your regex should use two positive `lookaheads` .
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
Your regex should not match `"astronaut"`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(!pwRegex.test('astronaut'));
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
Your regex should not match `"banan1"`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(!pwRegex.test('banan1'));
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
Your regex should match `"bana12"`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(pwRegex.test('bana12'));
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
Your regex should match `"abc123"`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(pwRegex.test('abc123'));
```
2021-02-06 04:42:36 +00:00
Your regex should not match `"12345"`
2020-12-16 07:37:30 +00:00
```js
2021-02-06 04:42:36 +00:00
assert(!pwRegex.test('12345'));
2020-12-16 07:37:30 +00:00
```
2021-02-06 04:42:36 +00:00
Your regex should match `"8pass99"`
2020-08-04 07:14:01 +00:00
2018-10-10 22:03:03 +00:00
```js
2021-02-06 04:42:36 +00:00
assert(pwRegex.test('8pass99'));
2018-10-10 22:03:03 +00:00
```
2020-08-04 07:14:01 +00:00
2021-02-06 04:42:36 +00:00
Your regex should not match `"1a2bcde"`
2020-12-16 07:37:30 +00:00
```js
2021-02-06 04:42:36 +00:00
assert(!pwRegex.test('1a2bcde'));
2020-12-16 07:37:30 +00:00
```
2021-02-06 04:42:36 +00:00
Your regex should match `"astr1on11aut"`
2020-12-16 07:37:30 +00:00
```js
2021-02-06 04:42:36 +00:00
assert(pwRegex.test('astr1on11aut'));
2020-12-16 07:37:30 +00:00
```
2021-01-13 02:31:00 +00:00
# --seed--
## --seed-contents--
```js
let sampleWord = "astronaut";
let pwRegex = /change/; // Change this line
let result = pwRegex.test(sampleWord);
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```js
2021-02-06 04:42:36 +00:00
let pwRegex = /(?=\w{6})(?=\w*\d{2})/;
2021-01-13 02:31:00 +00:00
```