Select sub strings at beginning and end

Tell us what’s happening:
Two part question.

This challenge felt like a real tricky one. Especially since the last few were using capture groups () in the regex and ,replacetext in a .replace() function.

So seeing the answers /^\s*|\s*$/g threw me off. If we’re trying to take it out why do we not have to capture it in the (). I tested it out and it works whether it’s in that format or this. /(^\s*)|(\s*$)/g

Second, with the last line, with the empty quotations
hello.replace(wsRegex, “”) why does leaving them empty result in hello = " Hello, World! "; becoming “Hello, World!” without the spaces at beginning and end.

Thanks for the clarification. This one stumped me and even the answer didn’t make the concept feel a whole lot clearer.

Your code so far

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

Your browser information:

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

Challenge: Remove Whitespace from Start and End

Link to the challenge:


^ - beginning of string
\s space
* zero or more
$ end of string
/g global (capture all matches)

so it basically says,
if you find at the beginning of the string of 1 or more spaces
if you find at the end of the string of 1 or more spaces

' Hello, World '.replace(that regex (matches), to ‘’ (empty string))

which effectively removes spaces from beginning and end of your string.

No idea why they didn’t just show you guys .trim() lmao.