Specify-upper-and-lower-number-of-matches

Tell us what’s happening:

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.

Link to the challenge:

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

1 Like

let ohStr = “Ohhh no”;
let ohRegex = /^h{3,6}/; // (/h{3,6}^h/ | /^h{3,6}/)
let result = ohRegex.test(ohStr);

Above cases is returning excluding the range 3-6…
Am i with you ?

1 Like

Hi there…

It worked if I mention the string completely with quantity specifiers including space… thats hectic if the string is big

let ohRegex = /Oh{3,6} no/; // Change this line

Sorry I meant something like [^h]h{3,6}[^h] (click to reveal)

[^h] matches anything that’s not h

2 Likes

What’s the point of having a range if it ignores the last number in the range? I don’t understand.

4 Likes

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

/Oh{3,6}[^h}/

10 Likes

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?

Solution
If you think about it step by step it actually is quite simple.

  • The first character is not h
  • The second position is an h, but it should only be selected if there are min 3 and max 6 h’s in a row
  • After the 6 h’s, there must not be another h
  • There are some other characters at the end

Try it with these steps.
Solution let ohRegex = /[^h]h{3,6}[^h]+/;

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.

I tested this on an online regexp checker and got expected behavior:

/Oh{3,6}[\s]/g

This should equate to match O, then followed by h ranging from 3 to 6 times followed by a newline character (space).

But the challenge in FCC is returning an error.

Exactly,that’s my point.

This is because of a slight misunderstanding:

the {} quantifier only matches the preceding token. And here you’re trying to match the preceding letter

/Oh{3,6}[\s]/g

If you wish to match a letter, or group of letters, then you need to create a token by grouping them with parentheses

/O(h){3,6}[\s]/g

Because regex is greedy and will match the longest string it can everytime, unless you add a quantifier after.

So this {3,6}

  • does not mean match only between 3 and 6
  • but means match a minimum of 3 to 6
6 Likes

Very nice explanation :slight_smile:

Oh,okay…Now I can get the point.
Thanks bro

I do not know why, but I received an answer only without the key g

/Oh{3,6}[\s]/