Remove Whitespace from Start and End using match()

Tell us what’s happening:
Describe your issue in detail here.
I would like to know why this method does not work when console.log shows “Hello, World!”
Am I overseeing something? Thank you!

Your code so far


let hello = "   Hello, World!  ";
let wsRegex = /\D+/g; // Change this line
let result = hello.match(wsRegex); // 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/95.0.4638.69 Safari/537.36

Challenge: Remove Whitespace from Start and End

Link to the challenge:

I’ve added console.log('"' + result + '"') at the end, and I’m still seeing spaces at the start and end of string…

\D matches not numbers, every thing in the string is not a number

1 Like

The console.log(result) shows:

[ '   Hello, World!  ' ]

clearly containing whitespaces on both ends.

Please refer here for RegEx expressions in JS: JavaScript RegExp Reference

Though you still have to work on the approach in general.

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