Question of removing whitespace

Tell us what’s happening:
Describe your issue in detail here.
Dear all,

I have question regarding /^\s+|\s+$/g, since the plus sign after ^\s and before $ already means the whitespace can be more than 1, why is it necessary to add g at the end.
Thanks for helping!

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15

Challenge: Remove Whitespace from Start and End

Link to the challenge:

the regex match spaces at the start OR at the end, not both at the same time. If there are spaces in both places teh regex would match only one of the two groups, the g is so that it doesn’t stop there and match the other group of spaces too

Hi ilenia, thank you so much! I now understand.

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