Skip to content

Commit 78e28e2

Browse files
fix: reset regex.lastIndex when tests use test method (freeCodeCamp#43695)
* added regex.lastIndex = 0 to tests * typo
1 parent 5c4e90d commit 78e28e2

11 files changed

+54
-0
lines changed

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/find-one-or-more-criminals-in-a-hunt.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ assert(
6969
Your regex should not match any criminals in the empty string `""`
7070

7171
```js
72+
reCriminals.lastIndex = 0;
7273
assert(!reCriminals.test(''));
7374
```
7475

7576
Your regex should not match any criminals in the string `P1P2P3`
7677

7778
```js
79+
reCriminals.lastIndex = 0;
7880
assert(!reCriminals.test('P1P2P3'));
7981
```
8082

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/ignore-case-while-matching.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,60 +23,70 @@ Write a regex `fccRegex` to match `freeCodeCamp`, no matter its case. Your regex
2323
Your regex should match the string `freeCodeCamp`
2424

2525
```js
26+
fccRegex.lastIndex = 0;
2627
assert(fccRegex.test('freeCodeCamp'));
2728
```
2829

2930
Your regex should match the string `FreeCodeCamp`
3031

3132
```js
33+
fccRegex.lastIndex = 0;
3234
assert(fccRegex.test('FreeCodeCamp'));
3335
```
3436

3537
Your regex should match the string `FreecodeCamp`
3638

3739
```js
40+
fccRegex.lastIndex = 0;
3841
assert(fccRegex.test('FreecodeCamp'));
3942
```
4043

4144
Your regex should match the string `FreeCodecamp`
4245

4346
```js
47+
fccRegex.lastIndex = 0;
4448
assert(fccRegex.test('FreeCodecamp'));
4549
```
4650

4751
Your regex should not match the string `Free Code Camp`
4852

4953
```js
54+
fccRegex.lastIndex = 0;
5055
assert(!fccRegex.test('Free Code Camp'));
5156
```
5257

5358
Your regex should match the string `FreeCOdeCamp`
5459

5560
```js
61+
fccRegex.lastIndex = 0;
5662
assert(fccRegex.test('FreeCOdeCamp'));
5763
```
5864

5965
Your regex should not match the string `FCC`
6066

6167
```js
68+
fccRegex.lastIndex = 0;
6269
assert(!fccRegex.test('FCC'));
6370
```
6471

6572
Your regex should match the string `FrEeCoDeCamp`
6673

6774
```js
75+
fccRegex.lastIndex = 0;
6876
assert(fccRegex.test('FrEeCoDeCamp'));
6977
```
7078

7179
Your regex should match the string `FrEeCodECamp`
7280

7381
```js
82+
fccRegex.lastIndex = 0;
7483
assert(fccRegex.test('FrEeCodECamp'));
7584
```
7685

7786
Your regex should match the string `FReeCodeCAmp`
7887

7988
```js
89+
fccRegex.lastIndex = 0;
8090
assert(fccRegex.test('FReeCodeCAmp'));
8191
```
8292

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/match-a-literal-string-with-different-possibilities.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,49 @@ Complete the regex `petRegex` to match the pets `dog`, `cat`, `bird`, or `fish`.
2525
Your regex `petRegex` should return `true` for the string `John has a pet dog.`
2626

2727
```js
28+
petRegex.lastIndex = 0;
2829
assert(petRegex.test('John has a pet dog.'));
2930
```
3031

3132
Your regex `petRegex` should return `false` for the string `Emma has a pet rock.`
3233

3334
```js
35+
petRegex.lastIndex = 0;
3436
assert(!petRegex.test('Emma has a pet rock.'));
3537
```
3638

3739
Your regex `petRegex` should return `true` for the string `Emma has a pet bird.`
3840

3941
```js
42+
petRegex.lastIndex = 0;
4043
assert(petRegex.test('Emma has a pet bird.'));
4144
```
4245

4346
Your regex `petRegex` should return `true` for the string `Liz has a pet cat.`
4447

4548
```js
49+
petRegex.lastIndex = 0;
4650
assert(petRegex.test('Liz has a pet cat.'));
4751
```
4852

4953
Your regex `petRegex` should return `false` for the string `Kara has a pet dolphin.`
5054

5155
```js
56+
petRegex.lastIndex = 0;
5257
assert(!petRegex.test('Kara has a pet dolphin.'));
5358
```
5459

5560
Your regex `petRegex` should return `true` for the string `Alice has a pet fish.`
5661

5762
```js
63+
petRegex.lastIndex = 0;
5864
assert(petRegex.test('Alice has a pet fish.'));
5965
```
6066

6167
Your regex `petRegex` should return `false` for the string `Jimmy has a pet computer.`
6268

6369
```js
70+
petRegex.lastIndex = 0;
6471
assert(!petRegex.test('Jimmy has a pet computer.'));
6572
```
6673

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/match-beginning-string-patterns.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ assert(calRegex.flags == '');
4343
Your regex should match the string `Cal` at the beginning of the string.
4444

4545
```js
46+
calRegex.lastIndex = 0;
4647
assert(calRegex.test('Cal and Ricky both like racing.'));
4748
```
4849

4950
Your regex should not match the string `Cal` in the middle of a string.
5051

5152
```js
53+
calRegex.lastIndex = 0;
5254
assert(!calRegex.test('Ricky and Cal both like racing.'));
5355
```
5456

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/match-ending-string-patterns.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ assert(lastRegex.flags == '');
4343
You should match `caboose` at the end of the string `The last car on a train is the caboose`
4444

4545
```js
46+
lastRegex.lastIndex = 0;
4647
assert(lastRegex.test('The last car on a train is the caboose'));
4748
```
4849

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/match-literal-strings.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ Complete the regex `waldoRegex` to find `"Waldo"` in the string `waldoIsHiding`
3838
Your regex `waldoRegex` should find the string `Waldo`
3939

4040
```js
41+
waldoRegex.lastIndex = 0;
4142
assert(waldoRegex.test(waldoIsHiding));
4243
```
4344

4445
Your regex `waldoRegex` should not search for anything else.
4546

4647
```js
48+
waldoRegex.lastIndex = 0;
4749
assert(!waldoRegex.test('Somewhere is hiding in this text.'));
4850
```
4951

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,48 +52,56 @@ assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
5252
Your regex should not match the string `astronaut`
5353

5454
```js
55+
pwRegex.lastIndex = 0;
5556
assert(!pwRegex.test('astronaut'));
5657
```
5758

5859
Your regex should not match the string `banan1`
5960

6061
```js
62+
pwRegex.lastIndex = 0;
6163
assert(!pwRegex.test('banan1'));
6264
```
6365

6466
Your regex should match the string `bana12`
6567

6668
```js
69+
pwRegex.lastIndex = 0;
6770
assert(pwRegex.test('bana12'));
6871
```
6972

7073
Your regex should match the string `abc123`
7174

7275
```js
76+
pwRegex.lastIndex = 0;
7377
assert(pwRegex.test('abc123'));
7478
```
7579

7680
Your regex should not match the string `12345`
7781

7882
```js
83+
pwRegex.lastIndex = 0;
7984
assert(!pwRegex.test('12345'));
8085
```
8186

8287
Your regex should match the string `8pass99`
8388

8489
```js
90+
pwRegex.lastIndex = 0;
8591
assert(pwRegex.test('8pass99'));
8692
```
8793

8894
Your regex should not match the string `1a2bcde`
8995

9096
```js
97+
pwRegex.lastIndex = 0;
9198
assert(!pwRegex.test('1a2bcde'));
9299
```
93100

94101
Your regex should match the string `astr1on11aut`
95102

96103
```js
104+
pwRegex.lastIndex = 0;
97105
assert(pwRegex.test('astr1on11aut'));
98106
```
99107

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,78 +29,91 @@ Change the regex `userCheck` to fit the constraints listed above.
2929
Your regex should match the string `JACK`
3030

3131
```js
32+
userCheck.lastIndex = 0;
3233
assert(userCheck.test('JACK'));
3334
```
3435

3536
Your regex should not match the string `J`
3637

3738
```js
39+
userCheck.lastIndex = 0;
3840
assert(!userCheck.test('J'));
3941
```
4042

4143
Your regex should match the string `Jo`
4244

4345
```js
46+
userCheck.lastIndex = 0;
4447
assert(userCheck.test('Jo'));
4548
```
4649

4750
Your regex should match the string `Oceans11`
4851

4952
```js
53+
userCheck.lastIndex = 0;
5054
assert(userCheck.test('Oceans11'));
5155
```
5256

5357
Your regex should match the string `RegexGuru`
5458

5559
```js
60+
userCheck.lastIndex = 0;
5661
assert(userCheck.test('RegexGuru'));
5762
```
5863

5964
Your regex should not match the string `007`
6065

6166
```js
67+
userCheck.lastIndex = 0;
6268
assert(!userCheck.test('007'));
6369
```
6470

6571
Your regex should not match the string `9`
6672

6773
```js
74+
userCheck.lastIndex = 0;
6875
assert(!userCheck.test('9'));
6976
```
7077

7178
Your regex should not match the string `A1`
7279

7380
```js
81+
userCheck.lastIndex = 0;
7482
assert(!userCheck.test('A1'));
7583
```
7684

7785
Your regex should not match the string `BadUs3rnam3`
7886

7987
```js
88+
userCheck.lastIndex = 0;
8089
assert(!userCheck.test('BadUs3rnam3'));
8190
```
8291

8392
Your regex should match the string `Z97`
8493

8594
```js
95+
userCheck.lastIndex = 0;
8696
assert(userCheck.test('Z97'));
8797
```
8898

8999
Your regex should not match the string `c57bT3`
90100

91101
```js
102+
userCheck.lastIndex = 0;
92103
assert(!userCheck.test('c57bT3'));
93104
```
94105

95106
Your regex should match the string `AB1`
96107

97108
```js
109+
userCheck.lastIndex = 0;
98110
assert(userCheck.test('AB1'));
99111
```
100112

101113
Your regex should not match the string `J%4`
102114

103115
```js
116+
userCheck.lastIndex = 0;
104117
assert(!userCheck.test('J%4'))
105118
```
106119

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
5252
Your regex should match the string `42 42 42`.
5353

5454
```js
55+
reRegex.lastIndex = 0;
5556
assert(reRegex.test('42 42 42'));
5657
```
5758

5859
Your regex should match the string `100 100 100`.
5960

6061
```js
62+
reRegex.lastIndex = 0;
6163
assert(reRegex.test('100 100 100'));
6264
```
6365

@@ -76,18 +78,21 @@ assert.equal('42 42'.match(reRegex.source), null);
7678
Your regex should not match the string `101 102 103`.
7779

7880
```js
81+
reRegex.lastIndex = 0;
7982
assert(!reRegex.test('101 102 103'));
8083
```
8184

8285
Your regex should not match the string `1 2 3`.
8386

8487
```js
88+
reRegex.lastIndex = 0;
8589
assert(!reRegex.test('1 2 3'));
8690
```
8791

8892
Your regex should match the string `10 10 10`.
8993

9094
```js
95+
reRegex.lastIndex = 0;
9196
assert(reRegex.test('10 10 10'));
9297
```
9398

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/specify-only-the-lower-number-of-matches.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ assert(haRegex.source.match(/{.*?}/).length > 0);
4141
Your regex should not match the string `Hazzah`
4242

4343
```js
44+
haRegex.lastIndex = 0;
4445
assert(!haRegex.test('Hazzah'));
4546
```
4647

4748
Your regex should not match the string `Hazzzah`
4849

4950
```js
51+
haRegex.lastIndex = 0;
5052
assert(!haRegex.test('Hazzzah'));
5153
```
5254

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/specify-upper-and-lower-number-of-matches.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ assert(ohRegex.source.match(/{.*?}/).length > 0);
3939
Your regex should not match the string `Ohh no`
4040

4141
```js
42+
ohRegex.lastIndex = 0;
4243
assert(!ohRegex.test('Ohh no'));
4344
```
4445

@@ -69,6 +70,7 @@ assert('Ohhhhhh no'.match(ohRegex)[0].length === 10);
6970
Your regex should not match the string `Ohhhhhhh no`
7071

7172
```js
73+
ohRegex.lastIndex = 0;
7274
assert(!ohRegex.test('Ohhhhhhh no'));
7375
```
7476

0 commit comments

Comments
 (0)