Regular Expressions - Remove Whitespace from Start and End

Tell us what’s happening:
why is this not correct but’ ```
/^\s+|\s+$/g


**Your code so far**


```javascript
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

@igorgetmeabrain

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