Why is my solution not correct? **SOLVED**

***Would not let me delete post but I found the issue

Hello,

I am a little confused as to why my solution does not work for this problem. I am not clear as to why the ending spaces are not being replaced as well.

My code produces : "Hello, World! " (Spaces remain on the end of string).

Any insight would be appreciated!

Your code so far

let hello = "   Hello, World!  ";
let wsRegex = /^(\s*)(.*)(\s*)$/; 
let result = hello.replace(wsRegex, "$2"); 
console.log(result);

My test results:

Your browser information:

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

Challenge: Remove Whitespace from Start and End

Link to the challenge:

1 Like
let hello = "   Hello, World!  ";
let wsRegex = /^(\s*)(.*)(\s*)$/; 
let result = hello.replace(wsRegex, "$2"); 
console.log(`"${result}"`);

No, it doesn’t produce the correct output.
Pay attention to the second group. It contains a greedy quantifier. So to fix it you may want to find out how to make it non-greedy.

Thank you!

I noticed afterward that the result was not correct, I just could not see the extra spaces at the end in the console output. Bad testing!

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