Hi guys if any one completed this challenge, please check my code.
Am i missing some thing here…
(no.of h’s search supposed to be in range 3-6… but it is true for anything more than 3)
Your code so far
let ohStr = "Ohhhhhhh no";
let ohRegex = /h{3,6}/; // Change this line
let result = ohRegex.test(ohStr);
alert (result);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.
Essentially what’s happening is that it’s matching on the first 3-6 hs even if there’s more than that, consider adding [^h] before and after what you have
Not sure if you’re still curious about this, but I spent some time trying to wrap my mind around regexes, so for others who might also be confused…
The last number in the range isn’t being ignored…the match method is searching for exactly what is specified, not knowing what “should” or “shouldn’t” occur after what has been specified. It is then returning that match.
So, with something like,
/Oh{3,6}/
we’ve said "hey, look for an “O” and then look for 3 to 6 “h’s”. But, we haven’t said, “hey, look for an “O”, and then stop if you’ve found 6 “h’s”.
So, “Ohhhhhh no”, “Ohhhhhhhhhhhhhhhhhhhhh no”, and Ohhhhhhqqqqqqqqq no” will all return “Ohhhhhh”
There are applications where this distinction is beneficial…just not so much when trying to stop matching h’s.
Anyways, to prevent a match when the next character is also an “h”, we need to tell the computer "hey, match an “O” and then match 3 to 6 “h’s”, and then continue matching UNLESS the next character is also an “h”:
Okay, but why do we need to specify the last character to return at all then? We could equally just write /h{3,}/ couldn’t we? I mean why would we specify the last matching character in a string two times?
As I understand it, /h{3,}/ won’t work in this particular problem because that would mean anything with over 3 h’s in a row would be matched. With /h{3,}/, something with one million h’s trailing would fit the criteria. We want a match of at least 3, but no more than 6.