Tell us what’s happening:
Describe your issue in detail here.
hints: useless/already understood and not helping much in this case.
Solutions: also useless as I don’t understand them.
I almost understand the first one, but why do we need to say that it must start with a number? As it is is works for all cases but " Your regex should not match the string 42 42 42 42" where it matches still because there’s still the 42 42 42 pattern in the string.
putting an $ on the last /1 not helping either.
Not matching in the case of more numbers was never covered in the exercise and so why is it in the test cases?
The second solution seems like something we were never taught and there are no explanations for it. Must have been pretty straightforward to whoever wrote it
Why can there either be a ‘!’ or not?
Your code so far
let repeatNum = "42 42 42";
let reRegex = /(\d+)\s\1\s\1/; // Change this line
let result = repeatNum.match(reRegex);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/18.104.22.168 Safari/537.36
Challenge: Regular Expressions - Reuse Patterns Using Capture Groups
I will start off by saying that I do not think those example answers are good (and please note that example answers are not the “official” or “best” answers – just answers that are known to pass the tests). While they pass all the tests, they do not truly meet the specification, and thus they would match a number of strings that they should not.
You can use regexr or similar sites any time you want to write and test a new regular expression. It is very helpful!
Okay, now let us look at the specification:
Use capture groups in reRegex to match a string that consists of only the same number repeated exactly three times separated by single spaces.
The expression given in the example for finding a repeated word ( /(\w+) \1 \1/ ) is very close to what we want, but obviously we want it to handle numbers only instead of all alphanumeric characters. We just need to replace \w+ with something to catch digits.
Once that is done, you will notice that it matches strings that include larger numbers at the left and right, or really any other characters outside of the three sets of digits; but at least it doesn’t catch tabs, newlines, and other forms of whitespace, like one of the example answers…
What is the proper method to ensure that the string starts and ends exactly with that pattern? You will need to add a certain character on the left, and a different character on the right.