* chore(learn): audit basic algorithm scripting * chore(learn): audit basic data structures * chore(learn): audit basic javascript * chore(learn): audit debugging * chore(learn): audit es6 * chore(learn): audit functional programming * chore(learn): audit intermidate algorithms * chore(learn): audit js projects * chore(learn): audit object oriented programming * chore(learn): audit regex * fix(learn): remove stray . * fix(learn): string to code * fix(learn): missed some * fix(learn): clarify strings Based on Randy's feedback, clarifies string instances where quotes were removed in favour of back ticks. * fix: apply suggestions - thanks Randy! :) Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: non-suggestion comments * chore(learn): remove comments from codes Removes the comments from the description and instruction code blocks to ensure that all relevant information is translatable. * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: revert crowdin fix * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * chore: change voice * fix: Christopher Nolan * fix: expressions would evaluate * fix: will -> would * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: to work to push * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
115 lines
2.7 KiB
Markdown
115 lines
2.7 KiB
Markdown
---
|
|
id: 587d7dba367417b2b2512ba9
|
|
title: Positive and Negative Lookahead
|
|
challengeType: 1
|
|
forumTopicId: 301360
|
|
dashedName: positive-and-negative-lookahead
|
|
---
|
|
|
|
# --description--
|
|
|
|
<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.
|
|
|
|
There are two kinds of lookaheads: <dfn>positive lookahead</dfn> and <dfn>negative lookahead</dfn>.
|
|
|
|
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.
|
|
|
|
```js
|
|
let quit = "qu";
|
|
let noquit = "qt";
|
|
let quRegex= /q(?=u)/;
|
|
let qRegex = /q(?!u)/;
|
|
quit.match(quRegex);
|
|
noquit.match(qRegex);
|
|
```
|
|
|
|
Both of these `match` calls would return `["q"]`.
|
|
|
|
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:
|
|
|
|
```js
|
|
let password = "abc123";
|
|
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
|
|
checkPass.test(password);
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
Use lookaheads in the `pwRegex` to match passwords that are greater than 5 characters long, and have two consecutive digits.
|
|
|
|
# --hints--
|
|
|
|
Your regex should use two positive `lookaheads`.
|
|
|
|
```js
|
|
assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
|
|
```
|
|
|
|
Your regex should not match the string `astronaut`
|
|
|
|
```js
|
|
assert(!pwRegex.test('astronaut'));
|
|
```
|
|
|
|
Your regex should not match the string `banan1`
|
|
|
|
```js
|
|
assert(!pwRegex.test('banan1'));
|
|
```
|
|
|
|
Your regex should match the string `bana12`
|
|
|
|
```js
|
|
assert(pwRegex.test('bana12'));
|
|
```
|
|
|
|
Your regex should match the string `abc123`
|
|
|
|
```js
|
|
assert(pwRegex.test('abc123'));
|
|
```
|
|
|
|
Your regex should not match the string `12345`
|
|
|
|
```js
|
|
assert(!pwRegex.test('12345'));
|
|
```
|
|
|
|
Your regex should match the string `8pass99`
|
|
|
|
```js
|
|
assert(pwRegex.test('8pass99'));
|
|
```
|
|
|
|
Your regex should not match the string `1a2bcde`
|
|
|
|
```js
|
|
assert(!pwRegex.test('1a2bcde'));
|
|
```
|
|
|
|
Your regex should match the string `astr1on11aut`
|
|
|
|
```js
|
|
assert(pwRegex.test('astr1on11aut'));
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
let sampleWord = "astronaut";
|
|
let pwRegex = /change/; // Change this line
|
|
let result = pwRegex.test(sampleWord);
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
let pwRegex = /(?=\w{6})(?=\w*\d{2})/;
|
|
```
|