Why is this incorrect

Tell us what’s happening:
Describe your issue in detail here.
I don’t understand why this is incorrect what I was thinking was putting the caret character that the first capture group would match the 2 spaces then I had (\w+) = hello so I put a ,\s\2! and a \1$ so It would get the last 2 spaces

Your code so far


let hello = "   Hello, World!  ";
let wsRegex = /^([^\s+])(\w+),\s\2!\1$/; // Change this line
let result = hello.match(wsRegex); // Change this line
console.log(result); 

Your browser information:

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

Challenge: Remove Whitespace from Start and End

Link to the challenge:

It doesn’t work because \s matches any whitespace, but you have negated the set by adding a ^ (caret) to its beginning.
Your regex also starts with a caret which means that the match must start from the beginning. This means that at the beginning of the string, there must be one or more non-whitespace characters, which isn’t the case.
Also, you need to put the + sign, if you intend it as a quantifier, immediately following the closing bracket. Having it inside means a literal match for the + sign, or the opposite if it’s negated.

Maybe this site https://regex101.com/ helps in testing.
Hope that helps.

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