Remove Whitespace from Start and End...Both cases in or

Tell us what’s happening:
/^\s+|\s+$/g is supposed to match and replace either ^\s+ or \s+$ …
.
But here, it’s matching and replacing both, Why ?

Your code so far


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


console.log(result);

let some = "aapp"
console.log(some.match(/aa|pp/g));


Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end

You have a global flag, that means it finds all matches

1 Like