Regular Expressions - Reuse Patterns Using Capture Groups

Tell us what’s happening:

I am really stuck at this exercise. Even the solutions make me confused.

Q1: “\s”can’t replace white space at all times?

I notice that in both solutions, there is a space between" (\d+)" and" \1", however when i replace the space with “\s” to write code like this "let reRegex = /^(\d+)\s\1\s\1$/ ;“my code fails as it does not meet this condition"Failed:Your regex should not match the string 42\t42\t42.”

Alternative attempt “let reRegex =/^(\d+)(\s)\1\2\1$/” fails as well due to the same reason " Failed:Your regex should not match the string 42\t42\t42."

Q2: What is (?!.) in the solution 2 intended to do? I think It is a negative lookahead to make sure there is no “.” Why do we need this negative lookahead here?

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36

Challenge: Regular Expressions - Reuse Patterns Using Capture Groups

Link to the challenge:

  • get rid of “\s” cause it could also mean “\t”
  • there are many ways a regEx can be constructed, using more shorthands means making it much simpler

btw, this is what i could muster by looking at let reRegex = /(\d{2,})([ ])\1\2\1$/; // Change this line this is not a complete solution but somewhat close to it, if you would like to consider to be so

see if that makes sense to you and re-arrange this in a way to pass this step, happy learning :slight_smile:

nb: if you would like to know more about regex consider looking into web cause there are some real good information such as tutorials and such, about it happy reading :slight_smile: https://regexone.com/

Could you please explain more about the need of (?!.) in solution 2? It looks for anything but dot. I don’t see the relevance of excluding dot.

i myself also very rookie in regex, but this resource might be helpful in describing what that notation means and more, happy reading :slight_smile: Advanced Regex Tutorial—Regex Syntax

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