Code seems to meet requirements but is not accepted as answer

Tell us what’s happening:
As shown in my code, I set my regex as /[^\s].+[^\s]/ to omit whitespace before and after any letters. I used let result = console.log(hello.match(wsRegex)); to check it and this was the outpput

[ ‘Hello, World!’,
index: 3,
input: ’ Hello, World! ',
groups: undefined ]

According to “[ ‘Hello, World!’,” this seems to imply the my regex did in fact remove the whitespace before and after Hello, World! but for some reason the requirement "result should be equal to the string Hello, World!" is not fulfilled. Please let me know where I went wrong with understanding the instructions or my attempt.

Your code so far


let hello = "   Hello, World!  ";
let wsRegex = /[^\s].+[^\s]/; // Change this line
let result = console.log(hello.match(wsRegex)); // Change this line

Your browser information:

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

Challenge: Remove Whitespace from Start and End

Link to the challenge:

You are setting result to console.log(...). You are getting a message in the console but what do you think result now equals?

Hi thanks for replying. Based on what the console output, result is an array.

So I tried changing it back to
let result = hello.match(wsRegex); // Change this line.
Yet I still do not meet the requirement " result should be equal to the string Hello, World!"

The challenge is expecting result to be a string.
String.match returns an object.

The match method returns information about the matches it finds in the string but it doesn’t actually make any changes to the string. Perhaps there is another method that would be more appropriate here? Something that might be able to replace any matches it finds with something else?

If you log out the array you get back, which part of it do you think you have to assign to the result variable? Which string at what index?

the value of console.log is undefined, so there is that issue

result should be a string based on the requirements

^I think/hope that was already established.

You literally only have to add 3 extra characters to pass the test. Think bracket notation and an index.

Thanks for the hint, I looked over the lessons and struggled to make one regex to view the beginning and end of the string for /s. I tried /^\s+ \s+$/ among many other combinations to try to get them in one go to no avail

let hello = "   Hello, World!  ";

let wsRegex = /^\s+/; // Change this line

let wrRegex = /\s+$/;

let result = hello.replace(wsRegex,""); // Change this line

result = result.replace(wrRegex,"");

console.log(hello.match(wsRegex));

console.log(result);

This gave the end result without spaces at the begging or end of the string but is probably not the way it was intended to be solved.

Thank you for correcting me on that, I completely missed that and saw it as an array

This is a perfect way to solve it, but you can make it cleaner with just one regular expression. You can combine both of your regular expressions using the OR (|) operator.

1 Like

Thank you! I’ll keep this in mind for future problems