Regex Alt Solution? Remove Whitespace from Start and End

I’m having trouble understanding why my solution isn’t accepted. My console log gives the result “Hello, World!” and it passes when I test it on regexr.com too. It seems I approached the problem in the opposite way as it was intended to be solved, but is there some larger issue with my code that I’m just not seeing?

let hello = "   Hello, World!  ";
let wsRegex = /(\w+\W)(\s)(\w+\W)/; 
let result = hello.replace(wsRegex, ""); 
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/109.0.0.0 Safari/537.36

Challenge: Regular Expressions - Remove Whitespace from Start and End

Link to the challenge:

Your result is blank.
(Your replace has replaced all the text with a blank so you end up with a number of blanks in result).

To see the blanks in the console, just use this log instead:

console.log("—"+result+"—");

That helped me see the whitespace!
- Hello, World! -
Now I see how I was going about it backwards, but I don’t understand why the console still shows “Hello, World!”

When I copied your code I saw a blank console.

Not sure why you don’t

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