One small thing

Tell us what’s happening:
I don’t quite understand the use of the | symbol. It means “or”…but in this imstance aren’t we replacing the whitespace from BOTH sides of the “Hello, World” text? Wouldn’t | indicate replacing one OR the other?

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 (X11; CrOS x86_64 13020.87.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.119 Safari/537.36.

Challenge: Remove Whitespace from Start and End

Link to the challenge:


The .replace() method, when used with a regex, replaces all parts of the string that match the regex. Your regex checks for whitespace at the beginning of a string OR whitespace at the end of the string, allowing both to match.


Hmm. OK that’s odd…no mention of that so far but I will accept it and move on. Thank you for helping.

1 Like

the g means that the regex should get all substrings that match it

if you have “all spaces at the beginning or all spaces at the end”, it will match first all spaces at the beginning, then it keep searching and it finds also the spaces at the end that match the pattern

like if you have /(ab)|(ba)/g, and you have a string like “abruptly the ball”, it will match, because there is the g, both the “ab” at the start of abruptly and the “ba” at the start of ball


Seems counter-intuitive but I’m brand new at this so I’ll just accept it. Thank you once again.