Problem with the "Remove Whitespace from Start and End" Challenge

Tell us what’s happening:
I already finished this challenge, but just because i tried every possible combination in the middle of it. What i mean by that is the OR operator: |. The way that i interpret it in this challenge is: match any whitespace (\s) that being 1 or more (+) at the beginin of the string (^) OR (|) match any whitespace (\s) that being 1 or more (+) at the end of the string ($). So that | for me is literally an “or” but what is actually doing is like saying AND.

The other thing i don’t really get is the g flag use in this case, kinda looks “redundant”.

Your code so far

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

console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0

Challenge: Remove Whitespace from Start and End

Link to the challenge:

I don’t think you’d want to use the equivalent of && here. Using the alternation operator removes extra white space in cases where it’s only at the beginning or ending of a string. With the equivalent of &&, you’d have to have extra white space at both the beginning and ending of a string.

The g flag is necessary for the regex to work more than once. Otherwise, the replace function simply removes the white space at the beginning of the string, but not at the end. The default is for regex to work once, which is why the g flag exists in the first place.

I think I understand now, and what it was missing in my way of thinking was the g flag use. Thank you for taking the time to answer this!