I have a question in an effort to understand. The , after (\w+, — why did you choose the comma? I don’t recall us learning about commas within captures (so far). We may have but I legitimately don’t remember it. How did you know to use it?
Crap! Okay, so it’s like when you need to make sure the exclamation point in a sentence is represented. I forgot all about punctuation within strings. Derp. Apparently I didn’t consider the , at all when building a solution.
Maybe fCC should create a challenge that explicitly makes sure we have to use a punctuation mark in the solution.
Hello I will try to explain first solution.
First, check out this link https://regex101.com/
let wsRegex = /^\s*(\w+,\s*\w+!)\s*$/;
^ asserts position at start of the string
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
1st Capturing Group (\w+,\s*\w+!)
\w+ matches any word character (equal to [a-zA-Z0-9_])
Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
, matches the character , literally (case sensitive)
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\w+ matches any word character (equal to [a-zA-Z0-9_])
Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
! matches the character ! literally (case sensitive)
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of the string
This Regualr Expression is selecting full string whitespaces included, but grouping “Hello, World!” into 1st group.
Then
let result = hello.replace(wsRegex,“$1”);
Replacing full string whitespaces included, with 1st group.
And that is it, I guess
This (third solution) worked for me but it was inspired with
jarabear’s post .
let hello = " Hello, World! “;
let wsRegex = /(^\s+)|(\s+$)/g; // Change this line
let result = hello.replace(wsRegex,”"); // Change this line
console.log(result);
Selecting only whitespaces in front and in the end of the string, then replacing selection with “” .
I have a question regarding the third solution. Specifically this right here: (^/s+)|(/s+$)
The | is used and this suggests to me that both sides of the code are not being specified for removal of whitespace; rather, whatever side the program deems best. Perhaps this issue is answered by the /g expression. Clarification, please?
Hello sorry for late reply,
String " Hello, World! " have 5 empty spaces, three in the front and two more after the string. So in order to select every empty space Regex flag /g Don’t return after first match is used.