Tell us what’s happening:
Thank you for your help!!!
The task is to write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.
// My solution - fails test
let hello = " Hello, World! ";
let wsRegex = /^(\s+)|\1$/g;
let result = hello.replace(wsRegex, "");
console.log("my result = " + result)
//my result = Hello, World!
i’m failed because i’m not removing the trailing whitespace.
i noticed that i’m not removing the trailing whitespace.
i thought the \1
is to match the first group.(\s+)
(the beginning whitespace)
but why the \1$
can’t match the trailing whitespace???
// FreeCodeCamp solution - passes test
let hello2 = " Hello, World! ";
let wsRegex2 = /^\s+|\s+$/g;
let result2 = hello2.replace(wsRegex2, "");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36
.
Challenge: Remove Whitespace from Start and End
Link to the challenge: