Specify Upper and Lower Number of Matches - global flag

Tell us what’s happening:
Hello,

I passed the test with the below answer without the g flag at the end. However, there are 2 questions I want to ask:

  1. I don’t understand why the test could not pass with the global flag at the end. This confuses my understanding of the use of g flag.

  2. [Spoiler Alert] The model answer solution used \s to match the space between “oh{3,6}” and “no”, while I just typed a whitespace in between and it still works. Is this a right code to use? Would this be a bad habit in long term?

First time posting, will appreciate all the help guys, thanks.

Your code so far


let ohStr = "Ohhh no";
let ohRegex = /Oh{3,6} no/g; // Change this line
let result = ohRegex.test(ohStr);

console.log(ohStr.match(ohRegex))

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/specify-upper-and-lower-number-of-matches

  1. I don’t think there is any problem in using the g tag, it should have passed the same way. Probably just FCC tests being too strict.
  2. \s will match spaces, tabs and line breaks while the space will only match a space. So it basically depends on the use case, do you want to match only the space or any white space characters?

Hi ghukahr, thank you very much for answering my question! I mistakenly thought that white space character == only the space bar, now I remember it also includes other special characters in \s. Thanks for reminding me!