Remove Whitespace from Start and End (is my solution bad?)

Tell us what’s happening:
So you can see my solution and it works, but it’s not being accepted. So the “correct” way is

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

is my solution worse even though it works??

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end

hey use trim() method it does the same job

let greeting = ' Hello world! '
greeting.trim()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim

1 Like

Yours replaces the first one or more characters of whitespace it finds, so it can only work if there is only one instance of whitespace you want to remove:

"    Hello, World!".replace(wsRegex, "")
// returns "Hello, World!"

"Hello, World!    ".replace(wsRegex, "")
// returns "Hello,World!    ", first whitespace is in the middle

"Hello,World!     ".replace(wsRegex, "")
// returns "Hello, World!", first whitespace is at the end

"    Hello, World!    ".replace(wsRegex, "")
// returns "Hello, World!    ", removes first bit of whitespace

The possible correct solution is saying “from the start of the string, match any characters of whitespace in a row OR match any characters of whitespace in a row that come just before the end of the string, and do this globally (ie for every instance that can be found)”

Yours is saying “match the first one [or more] characters of whitespace in a row”. Also you’re using a capture group (()) which is not necessary, although it won’t do anything here.

Hi, yes I forgot to mention that we weren’t supposed to use .trim() in this exercise :smiley: still, thanks for taking the time to read this :slight_smile:

if I added g though, then it should work right? As long as there’s more than 1 space. So /\s+/g

Almost, but that will remove all the whitespace, including anything in-between the words

So

^ is the start of a string, eg /^\d/ would match a digit at the start of a string, nothing else.
$ is the end of a string, eg /\d$/ would match a digit at the end of a string, nothing else.

So you don’t even need the g if you had two calls to replace - you could replace any whitespace at the start in the first one, then replace again to get any at the end.

But doing it in one, the solution is saying match either at the start or the end - that’s what the pipe character | is doing. And because that means that it will take whichever one matches, if it didn’t use g it would just match the first one that was true (so if there was whitespace at the beginning and end, it would only replace at the beginning, because that’s the first match that’s true). So g says get everything that matches, not just the first match.

Here is a short solution:

/\b.+[^\s]/