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