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

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

#1

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/


#2

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.


#3

You can try this:

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

#4

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.


#5

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


#6

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

Thanks for trying to help!


#7

No worries. Glad to hear it.


#8

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


#9

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


#10

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.


#11

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.


#12

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 " ".


#13

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');

#14

Hmm, what about this solution:

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


#15

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);

#16

…How about this brilliant solution?

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