Regular Expressions - Remove Whitespace from Start and End

Hi!! So I actually already passed this challenge a few times (I like to review these) and this time I did this solution and somehow it worked. I really want to know how it worked though; why did it only cut the end spaces and not the one in the middle (producing Hello,World!)? I always check my work with console.log and I actually submitted the work so see if it was right and it did indeed pass the tests. Thank you so much!!

My code:

let hello = "   Hello, World!  ";
let wsRegex = /(\s+)(\s+)/g; // Change this line
let result = hello.replace(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/113.0.0.0 Safari/537.36

Challenge: Regular Expressions - Remove Whitespace from Start and End

Link to the challenge:

You actually got lucky that the tests are not more robust. The reason it only remove the front and end spaces is that your regular expression only matches one or more spaces immediately followed by one or more spaces. The only two places that is true, is the front and end spaces. The middle has only a single space.

If the original string would have been " Hello, World! " with one space on each end, it would not have worked. Also, if the string would have had an extra space in the middle, it would have remove too many spaces.

Ohh, I see! Thank you so much!