Remove Whitespace from Start and End help

Remove Whitespace from Start and End help
0

#1

need help

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 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

ok so you want to remove something
read here about the replace function:

but you will need to make sure your regex is perfect…


#3

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


#4
let wsRegex = /^\s*(\S.*\S)\s*$/; // Change this line
let result = hello.replace(wsRegex,'$1'); // Change this line

or

let wsRegex = /(\S.*\S)/; // Change this line
let result = hello.match(wsRegex)[0]; // Change this line

#5

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?

I’ve been referencing W3School’s JS RegExp Reference page (to help me build my solutions) and they don’t list it there either.

From what I’ve done to rebuild your solution, the comma is somehow related to the "$1" but I’m not advanced enough to quite make the connection.

You way passes, obviously, but how did you decide to use that particular way? LOL I just want to understand :woman_student:t2:


#6

The comma represents a literal comma. jarbear had the following in the capture group:

(\w+,\s*\w+) - which means find 1 or more word characters, followed by a comma, followed by 0 or more white space characters, followed by 1 or more word characters.

jarbear was literally trying to capture the comma as part of the capture group. There is no extra use for commas in regular expressions other to represent the comma character literally.


#7

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. :upside_down_face: 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. :laughing:

Thanks for taking the time to explain this, @RandellDawson.


#8

FYI - It is not necessary to use a comma in the regular expression to solve this challenge.


#9

Oh, yeah I understand that. I was more or less curious to Jarabear’s way (because of the comma I forgot existed lol).


#10

Can you please explain both the solutions in detail?


#11

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 :slight_smile:

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 “” .


#12

@EmirUzunovic- Thanks for the link -https://regex101.com/ this is really helpful to visualize the regular expression.


#13

Not the best but easiest way to pass this last Q is:

let wsRegex = /Hello, World!/; 
let result = hello.match(wsRegex); 

#14

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?


#15

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.