Regular Expressions - Remove Whitespace from Start and End | Simpler solution?


I’ve found solution for Regular Expressions - Remove Whitespace from Start and End Challenge which I think is a little simpler than what is present as Solution in Guide Page.

I’ve checked it with multiple words, devided by multiple whitespaces and I think it works as it should. Am i right or am I missing something? Does .replace() has any kind of adventage in that case?

FreeCodeCamp proposed solution:

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

My solution:

let hello = "   Hello, World!  ";
let wsRegex = /\S+.*\S+/; // Change this line
let result = hello.match(wsRegex)[0]; // Change this line

Thank you in advance for you comments!

1 Like

Fine! I follow the same trail using the array properties. It works, but checking the FCC solution, I figure that this challenge is to be solved using the resources of the previous lessons on the module. Hence, the use of replace. Which never occurred to me either! Cheers.

Since we’re on the topic of alternate solutions, I’ll add the one I came up with, which is even simpler and does uses the .replace method:

let hello = " Hello, World! ";
let wsRegex = /\s\s+/g;
let result = hello.replace(wsRegex, “”);

It simply tells the computer to replace all the instances in which there are 2 or more whitespaces with nothing, so it only focuses on the spaces at the start and end.
But I realize this isn’t an ideal solution, as it wouldn’t work if there were multiple spaces one after the other within the sentence.

This may pass the test scenarios used in the challenge but it doesn’t work if you change the test string to

let hello = " Hello,  World! ";

Now you only have one space at the beginning and end of the string and thus your regexp won’t remove them.

1 Like

Ahh, I see. Good catch! Thank you!

Your solution can be fixed a bit, just use a Negative lookbehind statement and it will pass all freeCodeCamp tests.

	let hello = "   Hello, World!  ";
	let wsRegex =/(?<!,)\s+/g; // skip single space after symbol ,
	let result = hello.replace(wsRegex, '');

It can also be further modified to skip single spaces between words or numbers. It will look like this:

let hello = "    Hello,      World!       RegEx      is      cool.     ";
	let wsRegex = /(?<![,\!\w])\s+/g;  //   expanded by set [,\!\w]
	let result = hello.replace(wsRegex, '');
	console.log(result); // expect 'Hello, World! RegEx is cool.'

but still it is not a perfect solution, it has some drawbacks

Hello everyone,

Because I went on vacation for about two months after completing the Javasctipt unit, I’ve started the fCC course again and, now that I’ve gotten to this particular challenge, I came up with a different solution that’s working for the test cases. Let me know what you think and if you can come up with a test case that would make my proposed solution wrong. Cheers!

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

Basically, it matches everything that isn’t a whitespace, and accounts for the possible existence of whitespace inbetween words. And now that I’ve written that, I realize it would not work if there were more words separated by whitespace…

So never mind! Still, I’m gonna leave this up here in case it comes in handy for anybody