Does upper limit Matters in Regex quantity specifiers?

Tell us what’s happening:
The upper and lower limits are specified in code. But I see that it also matches if a character appears more times than upper limit.
.
Like {3,6} matches all characters appearing 3 or more times consecutively. But it also matches characters appearing more that 6 times.
If that’s how it was supposed to be, then whats the meaning of setting upper limit. I don’t find any use of it.

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 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

The code you’ve provided appears to be working exactly the way it should. Can you give an example of code where the regex is not recognizing the upper quantity limit or describe your question about the result in more detail?

1 Like

It does not do that. Example:

> /Oh{3,6}\sno/.test("Oh no")
false # 1 h
> /Oh{3,6}\sno/.test("Ohh no")
false # 2 h
> /Oh{3,6}\sno/.test("Ohhh no")
true # 3 h
> /Oh{3,6}\sno/.test("Ohhhh no")
true # 4 h
> /Oh{3,6}\sno/.test("Ohhhhh no")
true # 5 h
> /Oh{3,6}\sno/.test("Ohhhhhh no")
true # 6 h
> /Oh{3,6}\sno/.test("Ohhhhhhh no")
false # 7 h
> /Oh{3,6}\sno/.test("Ohhhhhhhh no")
false # 8 h
> /Oh{3,6}\sno/.test("Ohhhhhhhhh no")
false # 9 h
# ...and so on
2 Likes

Sorry, that was error in my browser, it works correctly.

Thanks ya’all for helping…