Replaceing swRegex. Why do you need to use |?

Read the hint and I understand, but the | is giving me trouble why do we need to use it? why can you not just use capture like this?

swRegex =(^\s+)(\s+$)  ?

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36.

Challenge: Remove Whitespace from Start and End

Link to the challenge:

Because without the pipe, the OR operator (|) it would be looking for beginning space that is right next to some end space - it would not account for stuff in between. With the OR operator, it knows that it is matching beginning space and it also separately find end space.

Regex is confusing. When I doubt, I go to a regex playground like this one and mess around with it.

Thank you, for the detailed explanation! It really helped.