Regular Expressions: Remove Whitespace from Start and End

Regular Expressions: Remove Whitespace from Start and End
0

#1

Hello everyone. I have been trying the following code so far to solve this challenge:

let hello = "   Hello, World!  ";
let wsRegex = /^\s*|\s*$/; // Change this line
let result = hello.replace(wsRegex,""); // Change this line

I have tried it also on jsbin.com and the result is “Hello, World!”, as expected. But on freeCodeCamp I pass two out of the three tests but the third says:
result should equal to “Hello, World!”. What am I doing wrong?
Thank you in advance.


#2

When you have problem with a challenge to provide a link to it will improve the pertinence of the answers :slight_smile:

Anyway, you’re testing for spaces at the start OR spaces at the end ^^
If you want the regex not to match just the first occurrence ( the spaces at the start i guess) you should use the g flag ^^


#3

Wow thank you so much. That solved it.