Cant make sense of "Reuse Patterns Using Capture Group"s

Ive read the related posts and comments on the topic but I still cant make sense of why things work the way they do in:

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

let repeatNum = "42 42 42";
let reRegex = /^(\d+)(\s)\1\s\1$/;  /* Change this line  - I dont understand why the ^$ characters are needed*/
let result = repeatNum.match(reRegex);

I got most sense from @aglope explanation but I still do not understand why does the (\d+) without using ^ match the second 42 and not the first one.

By that theory why does the following does not match only [42 42 42] but also match [42 42] and [42 42 42 42] and even [101 102 103] if I write just:

let repeatNum = "42 42 42";
let reRegex = /(\d+)(\s)\1/;
let result = repeatNum.match(reRegex);

Should not the g flag be needed to extract the 42 more than the specified amount of times? The topic explanantion states
" Using the .match() method on a string will return an array with the string it matches, along with its capture group."

Does it have something to do with that? I tried to run it all with both .math() and .test() but it behaves same. :sweat_smile: Is there some mechanism behind it that Im missing or am I overthinking this? :upside_down_face: :smile: Thank you ahead.

Your browser information:

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

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:

hi @katarina.r . Regex can be hard to grasp and even working for a while you can still fall in the pit of not getting some expression. Its good idea to try sites like https://regex101.com/ when you want to test different regular expressions.
On the particular matter you ask, match and test are meant to search for your expression in a whole string. For example you can put /42 42/.test('there is 42 42 in here!') which will return true, because there it finds 42 42 in the string. It doesnt match the whole string, but it finds what you are looking for. With the same logic, it wil lreturn true for /42 42/.test('i can find 42 42 inside 42 42 42 42 too'). Same goes for /42 42/.test('42 42 42 42'), or /(\d+)/s/1/.test('42 42 42 42'). But when i specify that not only it should find two consecutive identical numbers, but there shouldnt be more than two numbers in the string, it will only return so. /^(\d+)(\s)\1$/ will encapsulate the two numbers and return only a string which starts and end with whats written inside the expression. So /^42 42$/.test('i can find 42 42 inside 42 42 42 42, but i there are lot of other numbers and signs involved here :)') will return false. match() operates similarly, altho it will return an array which contains additional data on the result

5 Likes

no, this one will not be matched, as the capture group matches 101, and there is not an other 101 in the string for the \1 to match

the regex has the same number repeated 3 times, the g flag is for when you need to have the regex match more than the first instance of the pattern, in this case it’s not needed.
For example, if you have /gaga/ as a regex, and the string is "gagaregaga", if you do (/gaga/).match("gagaregaga") only the first instance of gaga is matched, if you add the g flag you get both of them

1 Like

Thank you so much for the super helpful explanantion and the link to the regex test resource! :slight_smile:

Thanks a lot for the explaining in detail. I was not able connect some dots before reading your explanation.

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