Regular Expressions - Remove Whitespace from Start and End

Tell us what’s happening:
Describe your issue in detail here.

This is a valid answer using regex! It should be accepted or the requirements should be updated :slight_smile: !

let hello = "   Hello, World!  ";
let wsRegex = / {2,}/; // Change this line
let result = hello.replace(wsRegex, ""); // Change this line

Your browser information:

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

Challenge: Regular Expressions - Remove Whitespace from Start and End

Link to the challenge:

I have added one line to your code to help you see that you have not passed the test:

let hello = "   Hello, World!  ";
let wsRegex = / {2,}/; // Change this line
let result = hello.replace(wsRegex, ""); // Change this line
console.log('"'+result+'"');

You can fix this code by adding one letter to the end of your regular expression; however, you should note that this is a poor solution that only works for this particular string, and it will not work in 99% of cases in which you need to trim whitespace from the beginning and end of a string.

Ideally, your solution should make use of the following: \s, ^, $, and |. You can play around with different regular expressions and different test strings here:

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