Remove Whitespace from Start and End (using capture group)

Tell us what’s happening:

Why doesnt it work?

I believed that my solution was following like this:

  1. caret stands for start
  2. (one or more white spaces)
  3. repeat capture group
    4)at the end

Your code so far


let hello = "   Hello, World!  ";
let wsRegex = /^(\s+)\1$/; // Change this line
let result = hello.replace(wsRegex, "0"); // Change this line
console.log(result)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.119.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end

your regex match a string of only spaces - maybe rethink a bit on what you want to match

Your replace also has a zero (“0”) as the replacement?

1 Like

yes, just to be able to see what is taking spaces off.