Removing trailing whitespaces

I’ve always found regex successful in blowing my head off. Anyways, finished the regex part of the course, and solved the last one (removing trailing spaces) by using two regex’s:

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

I have no idea why something like ^\s+|\s+$ works in detecting the trailing spaces but the replace method only removes the first match, i.e. in the beginning.

Any clarifications would be highly appreciated. Thanks.

If you use the global flag (g), it won’t return after the first match.

I wasn’t using the global modifier. The regex was successful in detecting spaces, what failed was the replace method. :neutral_face: