[Regex] Specify Upper and Lower Number of Matches - I believe my solution should work, why is it not?

<%= @topic_view.topic.title %>
<%= @topic_view.topic.average_rating %> <%= @topic_view.topic.posts.count { |p| !!p.custom_fields['rating'] } %>

Regex has been easy, and it still is, but I do not understand why my solution wouldn’t work here. One of the parameters you must pass is Your regex should not match "Ohhhhhhh no" and this is the one parameter my solution would not work with, meaning that /h{3,6}/ was matching the test with 7 of the letter h.

My Solution:

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

Working Solution From the Hints:

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

Why would this challenge only pass when you actually search /Oh{3,6}\sno? Is it something to do with how FCC’s IDE works? Please keep in mind that my solution did pass every parameter except the last one which I outlined above.

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

the test is good. The reason your regex does not match the last test is because what your saying when you write /Oh{3,6}/ is match me anything that has upper case O and between 3-6 characters of the letter h, and if you look at the last case
Ohhhhhhh no you are indeed matching between 3-6 of the letter h’s but this isnt what it wants. if you look at the correct regex that the test requires to pass /Oh{3,6}\sno/ this says… only return me a match that has between 3-6 letter h’s plus a space, which is denoted by /s followed by a character ‘n’ followed by a character ‘o’ and if you look at the examples does the last test example match this case? Do you see what I mean now?

Edit: basically the problem with what you wrote is that while it does indeed match things with a capital O followed by between 3-6 of the letter h, but it takes no account for any additional characters that may or may not come afterwards. hope this helps.

1 Like

THAT makes SO much more sense now. I didn’t think of the point that we need to limit the number of h’s so it doesn’t continue to search, thank you!