freeCodeCamp icon indicating copy to clipboard operation
freeCodeCamp copied to clipboard

Missing test cases: Reuse Patterns Using Capture Groups

Open plamoni opened this issue 3 years ago • 0 comments

On the Reuse Patterns Using Capture Groups, there's a missing test case that allows solutions that do not match the requirements.

The requirements read (emphasis added):

Use capture groups in reRegex to match a string that consists of only the same number repeated exactly three times separated by single spaces.

However, there's no test to confirm that a regex only matches single whitespace characters. Consider the following solution:

let reRegex = /^(\d+)\s+\1\s+\1$/;

This will match "42 42 42" and "42\t42\t42", neither of which meet the requirements of having a single space character delimiting the numbers.

I propose adding two additional test cases to prevent overly-permissive solutions:

Your regex should not match the string `42\t42\t42`.

```js
reRegex.lastIndex = 0;
assert(!reRegex.test('42\t42\t42'));
```
Your regex should not match the string `42  42  42`.

```js
reRegex.lastIndex = 0;
assert(!reRegex.test('42  42  42'));
```

See: https://github.com/freeCodeCamp/freeCodeCamp/blob/b35e8166c07604ad32618b3439916a754520bbcc/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups.md?plain=1#L38-L98

plamoni avatar Jan 27 '23 23:01 plamoni