Remove Whitespace from Start and End-gt

Tell us what’s happening:

When i declare wsRegex with space between | and\s it works like so:
let wsRegex = /^\s+| \s$/g;
if I do not put space : let wsRegex = /^\s+|\s$/g; the output has 1 space after Hello World such as for explanatory reason "Hello World! " and not “Hello World!” Not understanding the behavior.

Your code so far

let hello = "   Hello, World!  ";
let wsRegex = /^\s+| \s$/g; // Change this line
let result = hello.replace(wsRegex,'x'); // 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/76.0.3809.100 Safari/537.36.

Link to the challenge:

You are missing the quantifier (+) on the “at end” space matching. So without the literal space match you are only matching one space.

"  test".replace(/\s/, '')
" test"

"  test".replace(/ \s/, '')
1 Like