Remove Whitespace from Start and End Why this doesn't work in this test

Tell us what’s happening:

I have no idea why this code doesn’t work. I checked it in console and it did well. I don’t understand why this code doesn’t pass the tests.

Your code so far


let hello = "   Hello, World!  ";
let wsRegex = /\s/g; // 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:60.0) Gecko/20100101 Firefox/60.0.

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

Oh, It’s because that it only passes the current string. If I passed it any another string with a space in the middle, it will replace it as well.

You can try this:

let hello = " Hello, World! ";
if (hello===" Hello, World! "){hello = "Hello, World!";}
let result = hello;

I can’t. I need to use regex. Also this condition only works with this challenge and string so any another string will not work which is not the main purpose of this challenge…

Thank you for trying to help though.

OG, you’re right. Your current regex doesn’t work because it’s removing EVERY whitespace character from the string. But that’s not what you want. You want to remove all whitespace from the beginning and end of the string only.

Here are some links that I found helpful while doing this challenge. Read them and I suspect you’ll figure out how to write the appropriate regex:

https://regexone.com/lesson/whitespaces
https://regexone.com/lesson/line_beginning_end

1 Like

Nevermind. I forgot to say that I found the solution myself long time ago.

Thanks for trying to help!

No worries. Glad to hear it.

Passed that challenge after I came to know that , and ! are not alphanumeric. Anyway here’s the solution.
let hello = " Hello, World! ";
let wsRegex = /^\s+(\w+,\s\w+!)\s+$/; // Change this line
let result = hello.replace(wsRegex, ‘$1’); // Change this line

I mean, you could just use this ^\s+|\s$

2 Likes

let hello = " Hello, World! ";
let wsRegex = /\S+\s\S+/gi; // Change this line
let result = hello.match(wsRegex); // Change this line

This is the perfect sol basically.

Thanks for help but I tried long time ago that I solved it myself.

Also like I said before, you could just use ^\s+|\s$ as it is easier to read and maintain and shorter as well.

This wouldn’t work with any other types of strings, like " This is a string with more spaces " or " noSpace ". A better solution is:

let hello = " Hello, World! ";
let wsRegex = /^\s*(.*?)\s*$/;

\\ ^\s* = some number of leading spaces
\\ (.*?) = some number of any character, lazy
\\ \s*$ = some number of ending spaces

let result = hello.replace(wsRegex, ‘$1’);

This will eliminate any leading and ending spaces from any string, even " ".

1 Like

My solution to find first word or number after unknown number of empty spaces.

Click to show code
let wsRegex = /^\s+?(\w.*?)\s+?$/i;
let result = hello.replace(wsRegex, '$1');

Hmm, what about this solution:

let wsRegex = /(\s+)\s/g;
let result = hello.replace(wsRegex, “”);
It is work correctly and regex is short.

1 Like

here is the solution that’s work for me

let hello = "   Hello, World!  ";
let wsRegexmatch = /\w+,\s\w+!/g;  // Change this line
let match = hello.match(wsRegexmatch);
let result = hello.replace(hello,match);

…How about this brilliant solution?

let hello = "   Hello, World!  ";
let wsRegex = /Hello, World!/;
let result = hello.match(wsRegex);
1 Like

Hi everyone,
I know this solutions might sound silly, but I guess the logic isn’t wrong. I want to know the reason it doesn’t work:

let hello = "   Hello, World!  ";
let wsRegex = /\s(\w+)\s/;
let result = hello.replace(wsRegex, '$1');
console.log(result);

Can anyone help?
Thanks

one space, one or more alphanumeric characters (\w equals [a-zA-Z0-9_]), one space
the string doesn’t have this pattern anywhere

What if we add + after both s?
What does \w+ mean here?

Thanks

The \w will always mean “one of letters, numbers, underscore”
even if you add +, nowhere you have that exact pattern, as there is punctuation extra that your regex doesn’t contemplate