Remove Whitespace from Start and End Problem

Tell us what’s happening:
Hi, I’m not sure why this isn’t working. Can anyone help please? I know there are solutions out there but I’m not sure why this isn’t working.

As far as I can tell my regex pattern and capture groups are correct.

Your code so far


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

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

you wsRegex is matching the entire string (not just the spaces) because in the middle you have (.+) which means to match any characters…

try using www.regextester.com to work on your regex

1 Like

My intention was to capture everything in between the white space at the beginning and end of the string. Does the capture group instead include the whole string?

I will check that website out thanks

yes the current regex you are using matches the entire string

1 Like

There’s an easy way to fix your current regex if you only want to capture the middle part of the string (without the first and last spaces) but I don’t want to spoil the exercise for you. Regextester is a very handy resource for me so I hope it helps you equally well.

1 Like

Haha thanks for letting me know :slight_smile:

Yes, I was going to try that next, thanks. :slight_smile: