What is wrong with this regex question?! listed solution does not work

Tell us what’s happening:
Reloaded browser and took out the long comment i had inside to download later, and both solutions worked!! I fixed it myself.

I’ve made a few of my own solutions that work, including the commented out version of wsRegex .

I also tried the exact answer listed as the solution after at least an hour of trying this question, only to find out the listed solution does not work for me, either!

/* LISTED SOLUTION

let hello = "   Hello, World!  ";
let wsRegex = /^\s+|\s+$/g; // Change this line
let result = hello.replace(wsRegex, ""); // Change this line
`*/


**Your code so far**
        
```js

/*       Regular Expressions: Remove Whitespace from Start and End

Sometimes whitespace characters around strings are not wanted but are there. Typical processing of strings is to remove the whitespace at the start and end of it.

Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.

Note: The String.prototype.trim() method would work here, but you'll need to complete this challenge using regular expressions. */

let hello = "   Hello, World!  ";
let wsRegex = /^\s+|\s+$/g;// alt code below.
// let wsRegex = /(^\s+)(\w+\W)\s(\w+\W)(\s+$)/g; // this works and doesn't use .trim() but does NOT pass the test -_-; (when output text is "$2 $3")
let textStrip = ""
let result = hello.replace(wsRegex, textStrip);
hello.replace(wsRegex, textStrip) // Change this line
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0.

Challenge: Remove Whitespace from Start and End

Link to the challenge:

Hi there,

Hmm…just wondering, if you remove all codes and only add the following listed solution does it work?

let hello = "   Hello, World!  ";
let wsRegex = /^\s+|\s+$/g; // Change this line
let result = hello.replace(wsRegex, ""); // Change this line
2 Likes

That is exacly what I did and it worked. I believe my comment was detected by the test and it saw ‘.trim()’ and threw a fail.

1 Like

Yeah, copy-pasting and commenting can cause this kind of issue. It’s fairly common so nothing to fret about.

I’m glad that it worked for you.