Confused from `^` as it negates or selects the start of a line

Tell us what’s happening:
I am confused from the cases where the ^ negates and the other when it selects a word at the beginning of a line

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

Challenge: Remove Whitespace from Start and End

Link to the challenge:

It depends on the context it is used in. This page explains it quite well:

1 Like