Remove Whitespace from Start and End This should not be right

Tell us what’s happening:

This is a very different answer than the hint answer.

Your code so far

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

Link to the challenge:

Your regex:

\D* matches any character that\'s not a digit (equal to [^0-9])
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy

So your regex matches the entire var hello string, then you replace the whole string with a new string that does not have white space at the beginning and end. It’s not the intent of what the challenge is trying to teach so it should not pass, because you are removing the entire string (not just the white space) and replacing it with and entirely new string that does not have the white space.

You’ve looked at the hint so, in terms of learning try to understand the hint.

If you were to write a function to remove beginning and ending white space, what you have would not be a reusable function, it would only work for the specific “ Hello, World!” string. However, with the regex in the hint you could write a function that you could pass other string variables to and remove the white space.

If you want to submit the issue of your code passing, that should not, there is a way to do so on GitHub. Maybe if one of the moderators read this they can provide more specifics on it.

EDIT: I think the link for issues is here:

But check to make sure there isn’t already an open issue.

1 Like

Any challenge will allow you to pass by hardcoding test results. It’s not a useful way to solve the problem, but the tests will accept it.

1 Like