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