Skip to content

Missing test cases: Reuse Patterns Using Capture Groups  #49171

@plamoni

Description

@plamoni

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:

# --hints--
Your regex should use the shorthand character class for digits.
```js
assert(reRegex.source.match(/\\d/));
```
Your regex should reuse a capture group twice.
```js
assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
```
Your regex should match the string `42 42 42`.
```js
reRegex.lastIndex = 0;
assert(reRegex.test('42 42 42'));
```
Your regex should match the string `100 100 100`.
```js
reRegex.lastIndex = 0;
assert(reRegex.test('100 100 100'));
```
Your regex should not match the string `42 42 42 42`.
```js
assert.equal('42 42 42 42'.match(reRegex.source), null);
```
Your regex should not match the string `42 42`.
```js
assert.equal('42 42'.match(reRegex.source), null);
```
Your regex should not match the string `101 102 103`.
```js
reRegex.lastIndex = 0;
assert(!reRegex.test('101 102 103'));
```
Your regex should not match the string `1 2 3`.
```js
reRegex.lastIndex = 0;
assert(!reRegex.test('1 2 3'));
```
Your regex should match the string `10 10 10`.
```js
reRegex.lastIndex = 0;
assert(reRegex.test('10 10 10'));
```

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedOpen for all. You do not need permission to work on these.scope: curriculumLessons, Challenges, Projects and other Curricular Content in curriculum directory.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions