Reuse Patterns Using Capture Groups5

Tell us what’s happening:

Ok I know I am doing something wrong…but what?

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; 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

Challenge wants you following

Use capture groups in reRegex to match numbers that are repeated only three times in a string, each separated by a space.

But your pattern doesn’t work! So let me give some hint.

You need to get help of groups, as you did like (\d)
Note \w could math the chars like ‘A’ too, so you need another pattern to just grab the digits.
+ means 1 or more, not sure why did you added 1 after it! it’s not required.
\s\1 looks okay, but refer to the challenge again, it says 3 times! while it’s 2 times for you.

Feel free to ask for more hint comrade, but I believe you can make it now.

happy coding.

1 Like

let reRegex = /(\w+)\s\2/; // Change this line

This still doesnt work…I think I am missing d somewhere?

First \w might pass this test, but challenge asked for digits, so this is better to use \d

I’m sorry, I think I confused you, this \2 is wrong, becasue \2 mean point second group where there is no any second group.

Let me give more hint, considering following code which fixed of your patter, but still incompleted.

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

This mean anything regex captures(match) for first group, it should be next to it with a space, let’s look at it in more detail
(\d+) mean one or many digits together(like 1,90,2233,81801), and let’s name it group 1(becasue it’s the first group)
\s mean an space
\1 means exactly first group. So it mean if the first group was 9087, now we expect 9087 again(same content)
Possible matches: 77 77 , 10 10
possible not matching: 90 91 , 23 22 , 09 9

So it’s almost done pal, but the challenge wants you, first group is repeated three times, while the above matches are two repeats.

Now make it real.

Happy coding.

2 Likes

/(\d+)\s\1\s\1/

This code shows true for 42 42 42 42`
But the challenge is only for 3 numbers.

How can i fix it, so it show false for 4 same numbers.

Very good, this is becasue three 42 will match the pattern.

For the challenge, you may tell the regex check if it starts and end with this pattern

Hit: ^ means beginning of line, and $ means end of line

/^(\d+)\s\1\s\1$/
2 Likes

Thanks for your help but what about this

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);

Your code get me pass the challenge but if 3 consective digits are in middle than what?

2 Likes

12 42 42 42 123 won’t match the /^(\d+)\s\1\s\1$/, since the pattern expect one digit starts from the beginning(please mind the ^) should be repeated three times and no more (please mind the $).

detailed process, assuming matching 12 42 42 42 123 with /^(\d+)\s\1\s\1$/
^(\d+) means a digit just at start of the line, so it’s 12 and matches
\s means space which matches
\1 means exactly first group, but it’s 42, so it fails, and pattern return false, no more check for next data

then you may ignore ^ and $, so it lets the regex to match any string comes with same 3 consecutive digits

1 Like

like this
/(\d+)\s\1\s\1/
if yes then it show true to ‘42 42 42 42’ which i false as it is 4 same digits and challenge is for 3 only.