fix: reset regex.lastIndex when tests use test method (#43695)

* added regex.lastIndex = 0 to tests

* typo
This commit is contained in:
Ilenia
2021-10-06 09:14:50 +02:00
committed by GitHub
parent 5c4e90d24d
commit 78e28e2bd3
11 changed files with 54 additions and 0 deletions

View File

@ -69,12 +69,14 @@ assert(
Your regex should not match any criminals in the empty string `""` Your regex should not match any criminals in the empty string `""`
```js ```js
reCriminals.lastIndex = 0;
assert(!reCriminals.test('')); assert(!reCriminals.test(''));
``` ```
Your regex should not match any criminals in the string `P1P2P3` Your regex should not match any criminals in the string `P1P2P3`
```js ```js
reCriminals.lastIndex = 0;
assert(!reCriminals.test('P1P2P3')); assert(!reCriminals.test('P1P2P3'));
``` ```

View File

@ -23,60 +23,70 @@ Write a regex `fccRegex` to match `freeCodeCamp`, no matter its case. Your regex
Your regex should match the string `freeCodeCamp` Your regex should match the string `freeCodeCamp`
```js ```js
fccRegex.lastIndex = 0;
assert(fccRegex.test('freeCodeCamp')); assert(fccRegex.test('freeCodeCamp'));
``` ```
Your regex should match the string `FreeCodeCamp` Your regex should match the string `FreeCodeCamp`
```js ```js
fccRegex.lastIndex = 0;
assert(fccRegex.test('FreeCodeCamp')); assert(fccRegex.test('FreeCodeCamp'));
``` ```
Your regex should match the string `FreecodeCamp` Your regex should match the string `FreecodeCamp`
```js ```js
fccRegex.lastIndex = 0;
assert(fccRegex.test('FreecodeCamp')); assert(fccRegex.test('FreecodeCamp'));
``` ```
Your regex should match the string `FreeCodecamp` Your regex should match the string `FreeCodecamp`
```js ```js
fccRegex.lastIndex = 0;
assert(fccRegex.test('FreeCodecamp')); assert(fccRegex.test('FreeCodecamp'));
``` ```
Your regex should not match the string `Free Code Camp` Your regex should not match the string `Free Code Camp`
```js ```js
fccRegex.lastIndex = 0;
assert(!fccRegex.test('Free Code Camp')); assert(!fccRegex.test('Free Code Camp'));
``` ```
Your regex should match the string `FreeCOdeCamp` Your regex should match the string `FreeCOdeCamp`
```js ```js
fccRegex.lastIndex = 0;
assert(fccRegex.test('FreeCOdeCamp')); assert(fccRegex.test('FreeCOdeCamp'));
``` ```
Your regex should not match the string `FCC` Your regex should not match the string `FCC`
```js ```js
fccRegex.lastIndex = 0;
assert(!fccRegex.test('FCC')); assert(!fccRegex.test('FCC'));
``` ```
Your regex should match the string `FrEeCoDeCamp` Your regex should match the string `FrEeCoDeCamp`
```js ```js
fccRegex.lastIndex = 0;
assert(fccRegex.test('FrEeCoDeCamp')); assert(fccRegex.test('FrEeCoDeCamp'));
``` ```
Your regex should match the string `FrEeCodECamp` Your regex should match the string `FrEeCodECamp`
```js ```js
fccRegex.lastIndex = 0;
assert(fccRegex.test('FrEeCodECamp')); assert(fccRegex.test('FrEeCodECamp'));
``` ```
Your regex should match the string `FReeCodeCAmp` Your regex should match the string `FReeCodeCAmp`
```js ```js
fccRegex.lastIndex = 0;
assert(fccRegex.test('FReeCodeCAmp')); assert(fccRegex.test('FReeCodeCAmp'));
``` ```

View File

@ -25,42 +25,49 @@ Complete the regex `petRegex` to match the pets `dog`, `cat`, `bird`, or `fish`.
Your regex `petRegex` should return `true` for the string `John has a pet dog.` Your regex `petRegex` should return `true` for the string `John has a pet dog.`
```js ```js
petRegex.lastIndex = 0;
assert(petRegex.test('John has a pet dog.')); assert(petRegex.test('John has a pet dog.'));
``` ```
Your regex `petRegex` should return `false` for the string `Emma has a pet rock.` Your regex `petRegex` should return `false` for the string `Emma has a pet rock.`
```js ```js
petRegex.lastIndex = 0;
assert(!petRegex.test('Emma has a pet rock.')); assert(!petRegex.test('Emma has a pet rock.'));
``` ```
Your regex `petRegex` should return `true` for the string `Emma has a pet bird.` Your regex `petRegex` should return `true` for the string `Emma has a pet bird.`
```js ```js
petRegex.lastIndex = 0;
assert(petRegex.test('Emma has a pet bird.')); assert(petRegex.test('Emma has a pet bird.'));
``` ```
Your regex `petRegex` should return `true` for the string `Liz has a pet cat.` Your regex `petRegex` should return `true` for the string `Liz has a pet cat.`
```js ```js
petRegex.lastIndex = 0;
assert(petRegex.test('Liz has a pet cat.')); assert(petRegex.test('Liz has a pet cat.'));
``` ```
Your regex `petRegex` should return `false` for the string `Kara has a pet dolphin.` Your regex `petRegex` should return `false` for the string `Kara has a pet dolphin.`
```js ```js
petRegex.lastIndex = 0;
assert(!petRegex.test('Kara has a pet dolphin.')); assert(!petRegex.test('Kara has a pet dolphin.'));
``` ```
Your regex `petRegex` should return `true` for the string `Alice has a pet fish.` Your regex `petRegex` should return `true` for the string `Alice has a pet fish.`
```js ```js
petRegex.lastIndex = 0;
assert(petRegex.test('Alice has a pet fish.')); assert(petRegex.test('Alice has a pet fish.'));
``` ```
Your regex `petRegex` should return `false` for the string `Jimmy has a pet computer.` Your regex `petRegex` should return `false` for the string `Jimmy has a pet computer.`
```js ```js
petRegex.lastIndex = 0;
assert(!petRegex.test('Jimmy has a pet computer.')); assert(!petRegex.test('Jimmy has a pet computer.'));
``` ```

View File

@ -43,12 +43,14 @@ assert(calRegex.flags == '');
Your regex should match the string `Cal` at the beginning of the string. Your regex should match the string `Cal` at the beginning of the string.
```js ```js
calRegex.lastIndex = 0;
assert(calRegex.test('Cal and Ricky both like racing.')); assert(calRegex.test('Cal and Ricky both like racing.'));
``` ```
Your regex should not match the string `Cal` in the middle of a string. Your regex should not match the string `Cal` in the middle of a string.
```js ```js
calRegex.lastIndex = 0;
assert(!calRegex.test('Ricky and Cal both like racing.')); assert(!calRegex.test('Ricky and Cal both like racing.'));
``` ```

View File

@ -43,6 +43,7 @@ assert(lastRegex.flags == '');
You should match `caboose` at the end of the string `The last car on a train is the caboose` You should match `caboose` at the end of the string `The last car on a train is the caboose`
```js ```js
lastRegex.lastIndex = 0;
assert(lastRegex.test('The last car on a train is the caboose')); assert(lastRegex.test('The last car on a train is the caboose'));
``` ```

View File

@ -38,12 +38,14 @@ Complete the regex `waldoRegex` to find `"Waldo"` in the string `waldoIsHiding`
Your regex `waldoRegex` should find the string `Waldo` Your regex `waldoRegex` should find the string `Waldo`
```js ```js
waldoRegex.lastIndex = 0;
assert(waldoRegex.test(waldoIsHiding)); assert(waldoRegex.test(waldoIsHiding));
``` ```
Your regex `waldoRegex` should not search for anything else. Your regex `waldoRegex` should not search for anything else.
```js ```js
waldoRegex.lastIndex = 0;
assert(!waldoRegex.test('Somewhere is hiding in this text.')); assert(!waldoRegex.test('Somewhere is hiding in this text.'));
``` ```

View File

@ -52,48 +52,56 @@ assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
Your regex should not match the string `astronaut` Your regex should not match the string `astronaut`
```js ```js
pwRegex.lastIndex = 0;
assert(!pwRegex.test('astronaut')); assert(!pwRegex.test('astronaut'));
``` ```
Your regex should not match the string `banan1` Your regex should not match the string `banan1`
```js ```js
pwRegex.lastIndex = 0;
assert(!pwRegex.test('banan1')); assert(!pwRegex.test('banan1'));
``` ```
Your regex should match the string `bana12` Your regex should match the string `bana12`
```js ```js
pwRegex.lastIndex = 0;
assert(pwRegex.test('bana12')); assert(pwRegex.test('bana12'));
``` ```
Your regex should match the string `abc123` Your regex should match the string `abc123`
```js ```js
pwRegex.lastIndex = 0;
assert(pwRegex.test('abc123')); assert(pwRegex.test('abc123'));
``` ```
Your regex should not match the string `12345` Your regex should not match the string `12345`
```js ```js
pwRegex.lastIndex = 0;
assert(!pwRegex.test('12345')); assert(!pwRegex.test('12345'));
``` ```
Your regex should match the string `8pass99` Your regex should match the string `8pass99`
```js ```js
pwRegex.lastIndex = 0;
assert(pwRegex.test('8pass99')); assert(pwRegex.test('8pass99'));
``` ```
Your regex should not match the string `1a2bcde` Your regex should not match the string `1a2bcde`
```js ```js
pwRegex.lastIndex = 0;
assert(!pwRegex.test('1a2bcde')); assert(!pwRegex.test('1a2bcde'));
``` ```
Your regex should match the string `astr1on11aut` Your regex should match the string `astr1on11aut`
```js ```js
pwRegex.lastIndex = 0;
assert(pwRegex.test('astr1on11aut')); assert(pwRegex.test('astr1on11aut'));
``` ```

View File

@ -29,78 +29,91 @@ Change the regex `userCheck` to fit the constraints listed above.
Your regex should match the string `JACK` Your regex should match the string `JACK`
```js ```js
userCheck.lastIndex = 0;
assert(userCheck.test('JACK')); assert(userCheck.test('JACK'));
``` ```
Your regex should not match the string `J` Your regex should not match the string `J`
```js ```js
userCheck.lastIndex = 0;
assert(!userCheck.test('J')); assert(!userCheck.test('J'));
``` ```
Your regex should match the string `Jo` Your regex should match the string `Jo`
```js ```js
userCheck.lastIndex = 0;
assert(userCheck.test('Jo')); assert(userCheck.test('Jo'));
``` ```
Your regex should match the string `Oceans11` Your regex should match the string `Oceans11`
```js ```js
userCheck.lastIndex = 0;
assert(userCheck.test('Oceans11')); assert(userCheck.test('Oceans11'));
``` ```
Your regex should match the string `RegexGuru` Your regex should match the string `RegexGuru`
```js ```js
userCheck.lastIndex = 0;
assert(userCheck.test('RegexGuru')); assert(userCheck.test('RegexGuru'));
``` ```
Your regex should not match the string `007` Your regex should not match the string `007`
```js ```js
userCheck.lastIndex = 0;
assert(!userCheck.test('007')); assert(!userCheck.test('007'));
``` ```
Your regex should not match the string `9` Your regex should not match the string `9`
```js ```js
userCheck.lastIndex = 0;
assert(!userCheck.test('9')); assert(!userCheck.test('9'));
``` ```
Your regex should not match the string `A1` Your regex should not match the string `A1`
```js ```js
userCheck.lastIndex = 0;
assert(!userCheck.test('A1')); assert(!userCheck.test('A1'));
``` ```
Your regex should not match the string `BadUs3rnam3` Your regex should not match the string `BadUs3rnam3`
```js ```js
userCheck.lastIndex = 0;
assert(!userCheck.test('BadUs3rnam3')); assert(!userCheck.test('BadUs3rnam3'));
``` ```
Your regex should match the string `Z97` Your regex should match the string `Z97`
```js ```js
userCheck.lastIndex = 0;
assert(userCheck.test('Z97')); assert(userCheck.test('Z97'));
``` ```
Your regex should not match the string `c57bT3` Your regex should not match the string `c57bT3`
```js ```js
userCheck.lastIndex = 0;
assert(!userCheck.test('c57bT3')); assert(!userCheck.test('c57bT3'));
``` ```
Your regex should match the string `AB1` Your regex should match the string `AB1`
```js ```js
userCheck.lastIndex = 0;
assert(userCheck.test('AB1')); assert(userCheck.test('AB1'));
``` ```
Your regex should not match the string `J%4` Your regex should not match the string `J%4`
```js ```js
userCheck.lastIndex = 0;
assert(!userCheck.test('J%4')) assert(!userCheck.test('J%4'))
``` ```

View File

@ -52,12 +52,14 @@ assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
Your regex should match the string `42 42 42`. Your regex should match the string `42 42 42`.
```js ```js
reRegex.lastIndex = 0;
assert(reRegex.test('42 42 42')); assert(reRegex.test('42 42 42'));
``` ```
Your regex should match the string `100 100 100`. Your regex should match the string `100 100 100`.
```js ```js
reRegex.lastIndex = 0;
assert(reRegex.test('100 100 100')); assert(reRegex.test('100 100 100'));
``` ```
@ -76,18 +78,21 @@ assert.equal('42 42'.match(reRegex.source), null);
Your regex should not match the string `101 102 103`. Your regex should not match the string `101 102 103`.
```js ```js
reRegex.lastIndex = 0;
assert(!reRegex.test('101 102 103')); assert(!reRegex.test('101 102 103'));
``` ```
Your regex should not match the string `1 2 3`. Your regex should not match the string `1 2 3`.
```js ```js
reRegex.lastIndex = 0;
assert(!reRegex.test('1 2 3')); assert(!reRegex.test('1 2 3'));
``` ```
Your regex should match the string `10 10 10`. Your regex should match the string `10 10 10`.
```js ```js
reRegex.lastIndex = 0;
assert(reRegex.test('10 10 10')); assert(reRegex.test('10 10 10'));
``` ```

View File

@ -41,12 +41,14 @@ assert(haRegex.source.match(/{.*?}/).length > 0);
Your regex should not match the string `Hazzah` Your regex should not match the string `Hazzah`
```js ```js
haRegex.lastIndex = 0;
assert(!haRegex.test('Hazzah')); assert(!haRegex.test('Hazzah'));
``` ```
Your regex should not match the string `Hazzzah` Your regex should not match the string `Hazzzah`
```js ```js
haRegex.lastIndex = 0;
assert(!haRegex.test('Hazzzah')); assert(!haRegex.test('Hazzzah'));
``` ```

View File

@ -39,6 +39,7 @@ assert(ohRegex.source.match(/{.*?}/).length > 0);
Your regex should not match the string `Ohh no` Your regex should not match the string `Ohh no`
```js ```js
ohRegex.lastIndex = 0;
assert(!ohRegex.test('Ohh no')); assert(!ohRegex.test('Ohh no'));
``` ```
@ -69,6 +70,7 @@ assert('Ohhhhhh no'.match(ohRegex)[0].length === 10);
Your regex should not match the string `Ohhhhhhh no` Your regex should not match the string `Ohhhhhhh no`
```js ```js
ohRegex.lastIndex = 0;
assert(!ohRegex.test('Ohhhhhhh no')); assert(!ohRegex.test('Ohhhhhhh no'));
``` ```