Regular Expressions - Remove Whitespace from Start and End

Tell us what’s happening:
why is this not correct but’ ```

**Your code so far**

let hello = "   Hello, World!  ";
let wsRegex = /^\s+|$s+/g; // Change this line//
let result = hello; // Change this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0

Challenge: Regular Expressions - Remove Whitespace from Start and End

Link to the challenge:

The first part of your regex is correct but the part after the OR will not match whitespace because of a typo.
Also, you need to change the value of the result variable, otherwise you’re simply returning the original string.

1 Like


Because you can think of:

  • ^ as a “character” only present at the beginning of the line,
  • $ same but at the end of the line .

They are not, but that is how we can make sense of the notation.

The only problem with the regex is with the character it’s attempting to match not with the caret or dollar denoting beginning/end of string.

Sorry I tagged the wrong user, I think if you read carefully OP meant why you write ^something and then not $something as if ^ and $ meant that those words are at the beginning and the end.

Makes sense the question to me. The missing \ is probably known by OP or not the main issue anyways.

so you are saying that $ should be put at the end of the command

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