Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.7 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dba367417b2b2512ba9 Positive and Negative Lookahead 1 301360 positive-and-negative-lookahead

--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";
let noquit = "qt";
let quRegex= /q(?=u)/;
let qRegex = /q(?!u)/;
quit.match(quRegex); // Returns ["q"]
noquit.match(qRegex); // Returns ["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:

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

--instructions--

Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits.

--hints--

Your regex should use two positive lookaheads.

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

Your regex should not match "astronaut"

assert(!pwRegex.test('astronaut'));

Your regex should not match "banan1"

assert(!pwRegex.test('banan1'));

Your regex should match "bana12"

assert(pwRegex.test('bana12'));

Your regex should match "abc123"

assert(pwRegex.test('abc123'));

Your regex should not match "1234"

assert(!pwRegex.test('1234'));

Your regex should not match "8pass99"

assert(!pwRegex.test('8pass99'));

Your regex should not match "12abcde"

assert(!pwRegex.test('12abcde'));

Your regex should match "astr1on11aut"

assert(pwRegex.test('astr1on11aut'));

--seed--

--seed-contents--

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

--solutions--

var pwRegex =  /^\D(?=\w{5})(?=\w*\d{2})/;