Remove Whitespace from Start and End[SOLVED]

Tell us what’s happening:
I am stuck with this and tried several Regex to solve this code.

Your code so far


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

I also tried /^\s*$/ to try and match whitespace at the beginning and the end but it’s not working. Please give me a hint.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

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

2 Likes

Right now your pattern translates to this: “start of line, zero or more whitespace characters, zero or more whitespace characters, end of line”. It would only match strings that have no non-whitespace characters.

2 Likes

Okay , Thanks for the hint. I realized my mistakes when I reread my Regex

So what is the correct solution? I got to basically the same code you got to but do not know how to proceed to the correct solution. Help please!

1 Like

Basically the code posted above is part of it, but you also have to match the parts in between the opening and ending spaces, the “Hello, World!” part. Here’s how you do it:

let hello = "   Hello, World!  ";
let wsRegex = /^\s*\w+,\s\w+!\s*/; // By using \w+, you are looking for all matching alphabetical letters, then you simply need to drop in the comma, as well as the space after the comma, as well as the exclamation mark.
let result = hello.replace(wsRegex, 'Hello, World!'); // Change this line
5 Likes

@cknight904 try @EloquentSyntax’s solution. :slight_smile:

1 Like

This solution works, but what the regex is doing is it is selecting the whole string and replacing with the trimmed string.

Is there a regex for selecting just the beginning and ending spaces and removing them?

2 Likes

How about this?

You’re right that the correct approach is not to replace the entire string. This regex doesn’t do you much good if it requires you to know what it contains for it to do any good. regex101.com/ is very helpful. I recommend looking at the Quick Reference sections for Anchors and Group Constructs.

1 Like

This is the solution i came up with if anyone is interested:

 let wsRegex = /^\s*(.*\S)\s*$/; // Change this line
let result = hello.replace(wsRegex, "$1"); // Change this line
15 Likes

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

2 Likes

I solved it differently and it worked. Not sure this is right, though. Any feedback is appreciated:

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

Right now your pattern only works if the string is supposed to be "Hello, World!". If you are only changing that one string, then it would be much easier and more efficient to just do

let result = "Hello, World!";

The regular expression doesn’t really do anything for you. The goal here is that you could use the same pattern on "Goodbye World" or "Let's grab dinner and a movie tonight." and you would always get that string with the whitespace trimmed.

2 Likes

Thanks for your feedback Ariel. You’re right! I will try once again!

1 Like

I searched forever and spent far too long on this. From other posts I have read they are writing this regex to only work for this one specific string. However, the instructions does not ask for this. This is my solution that works with any strings I have tested so far.

let wsRegex = /\S.*\S/;
let result = hello.match(wsRegex);
15 Likes

This is a great solution.

I tried simplifying even further by putting the regex in a capture group and then referring to the capture group in the replace method, but this didn’t work. Does anyone have any idea why?

let wsRegex = /(\S.*\S)/;
str.replace(wsRegex, '$1');

First it’s important to note the following:
DaxHarley uses .match() to match the string and exclude spaces(\S) before and after anything else (.*).

To address your proposed solution:
You are currently replacing something by itself. The code your wrote currently equals: str.replace(wsRegex, '$1'); is the same as str.replace(/\S.*\S/, /\S.*\S/);.

There is no real reason to use a capture group here. Imho the best solutions are:

using .match():

let wsRegex = /\S.*\S/;
let result = hello.match(wsRegex);

using .replace():

let wsRegex =/^\s*|\s*$/g;
let result = hello.replace(wsRegex, “”);

or ofcourse in real life where you would use .trim() :slight_smile:

20 Likes

Ah okay that makes sense, thanks for your reply. I got as far as your code except I didn’t include | (which I forgot existed, ha), but that’s a very elegant solution that actually makes sense to me as well! Thanks :slight_smile:

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

2 Likes

I passed mine with this

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