Remove Whitespace from Start and End Problem

Remove Whitespace from Start and End Problem
0

#1

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


#2

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


#3

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?


#4

I will check that website out thanks


#5

yes the current regex you are using matches the entire string


#6

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.


#7

Haha thanks for letting me know :slight_smile:


#8

Why not try replacing one or more white spaces characters and the beginning of the string OR at the end of the string with a blank string?

HINT: The key word here is OR.


#9

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