Why doesn't this solution work? [Specify Upper and Lower Number of Matches]

“Specify Upper and Lower Number of Matches”


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

The result is

// running tests
Your regex should not match the string Ohhhhhhh no
// tests completed

Challenge: Specify Upper and Lower Number of Matches

Link to the challenge:

The . in regex matches any character. Would h be any character?

The . in my solution would match any first character, which is “O”
What I’m having trouble with is why doesn’t my [^h]+ work? I understand it as “match anything that is not “h” until the end of the string”

as your regex does not have any indication on start or end of string, the . matches an “h”, and the h{3,6} matches the other 6, so the [^h]+, after the "h"s, no other “h”, but before? . matches any character, “h” included

2 Likes

I’m sorry but why . would match an “h” and not the “O”?

And i didn’t quite understand this part.
Thank you

Because its just shifting over and the . will find the first h, and h{3,6} will then find 3-6 more h’s, so possibly 7 h’s. Then [^h]+ then finds the no

1 Like

I get it now.
Thank you

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.