Regular Expressions - Reuse Patterns Using Capture Groups

Tell us what’s happening:
Describe your issue in detail here.
I don’t understand what am I doing wrong here. can anyone help ??
Your code so far

let repeatNum = "42 42 42";
let reRegex = /(\d+) \1 \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/107.0.0.0 Safari/537.36

Challenge: Regular Expressions - Reuse Patterns Using Capture Groups

Link to the challenge:

You are almost there! Remember that you are asked to match only strings that are exactly one number repeated three times with spaces in between – no more or less. For example, you should not match "10 10 100" or "hello 5 5 5 goodbye".

What can you do to ensure that you only match an entire string, and not a substring of a larger string?

I tried this as well: (\d+) \1 \1$; but it says regex should not match the string 42 42 42 42

The dollar sign means that nothing can come after the expression, but does not affect what can come before. That means that in the case of 42 42 42 42, it will match the final three 42s.

You need to add one more symbol to indicate that nothing can come before the expression.

1 Like

Thank you…!
I added caret sign in the beginning it worked. I had already tried using those signs but separately so that’s why the test was failing.

In the future, I recommend using a website like https://regexr.com/ or https://www.regexpal.com/ to help you when developing regular expressions. You can put in a test string and see exactly what does or does not match, you can mouseover symbols in the expression to get an explanation of their function, and regexr can additionally allow you to set up a series of tests just like the ones you have to pass on FCC.

These tools can save you a lot of headaches!

(I personally prefer regexr because it has many more useful features)

Hi. I have the same problem. Cannot understand why the caret works here.

In the previous challenges caret was explained as follows: To create a negated character set, you place a caret character (^ ) after the opening bracket and before the characters you do not want to match.

Why /^(\d+) \1 \1$/ works for the task “match a string that consists of only the same number repeated exactly three times” if the caret is meant for “characters you do not want to match.” ?

1 Like

How the ^ works changes depending on where it is used in the regex. In the one you just posted ^ is saying “this needs to be at the beginning of the string”. But in [^a] its saying “match anything that is not the letter a”.

I normally use https://regex101.com/ to test and understand what a regex is doing, because it breaks it down into nice pieces in the top right that help me understand everything going on in it. it also has a nice cheat sheet in the lower right.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.