Regular Expressions - Remove Whitespace from Start and End

Tell us what’s happening:
So I don’t know if I correctly understand what I’m to do. I tried capturing each word and using just that for the replace, and now I tired this and my thoughts are that I’m just capturing the string “Hello, World!” without the spaces and when I replace it with that group it doesn’t use the spaces. Should I be trying to capture only the white space? I tried to have wsRegex =^(\s)$ . sp the replace could just be ‘’ getting rid of the white space at the beginning and end. Sorry for the long post I’m just trying to get more information since I didn’t really find the hints to helpful and I don’t want to be spoiled on the answer. Advice is appreciated.

Your code so far

let hello = "   Hello, World!  ";
let wsRegex = /^\s(\w+\W\s\w+\W)\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; rv:109.0) Gecko/20100101 Firefox/109.0

Challenge: Regular Expressions - Remove Whitespace from Start and End

Link to the challenge:

i am sure there are multiple ways to do this. One way is to ‘match’ the text and ignore the rest.

If you can get your pattern to match the words and ignore the spaces at the start and end, then you can grab that match from the returned array value of the match method

Thank you for your reply. I ended up giving up and looking at the given solution. The funny thing is I tried to do something very similar I just didn’t use OR as well as the + sign. I kept forgetting that I needed to account for the space occurring more than once. Anyway thank you for the reply, it’s frustrating though, I really felt good through most or this unit too but it’s problems like these that make me think I really didn’t understand anything I learned.sorry for the rant and thank you again for the reply.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.