Reuse Patterns Using Capture Groups Not working

Reuse Patterns Using Capture Groups Not working
0

#1

Tell us what’s happening:
Not working for 4 numbers like
42 42 42 42
It shows true for that too.

Your code so far


let repeatNum = "42 42 42 ";
let reRegex = /(\d+)\s\1\s\1/; // Change this line
let result = reRegex.test(repeatNum);
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups/


#2

You need to rewrite your regex to force it to capture only 3 occurrences of 42

If 4 occurrences you must fail.

Use what you know about reg. expressions to force this to happen. This is a hint. You can search the forum if you want as well to see how others did that.


#3

hbar1st:

By force if you mean {3} then i tried it but it didn’t work.
like this
/(\d+)\s\1{3}/
& also
/((\d+)\s\1)){3}/
/(\d+{3})\s\1/
etc


#4

yes unfortunately that is not what they are looking for.

one more hint:
how do you make a regex that must start at the beginning of a string?
how do you make a regex that must end at the end of a string?

hope these help you along. And as I mentioned, the forum has more if you search…


#5

let repeatNum = “12 42 42 42 123”;
let reRegex = /^(\d+)\s\1\s\1$/; // Change this line
let result = reRegex.test(repeatNum);
console.log(result);

this trick won’t work if 3 same digit are in middle.
Anyway to make things clear.