Specify Upper and Lower Number of Matches RegExp error? [SOLVED]

Specify Upper and Lower Number of Matches RegExp error? [SOLVED]
0

#1

Tell us what’s happening:

I tried this code using brackets.io and it works. But in freecodecamp, it says :

// running test
Your regex should match “Ohhhh no”
Your regex should match “Ohhhhhh no”
// tests completed

Did I do this wrong? anyone can give me a hint?
Your code so far


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

Your browser information:

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


#2

Why is this code wrong in Freecodecamp but when I tried it brackets with .test() method and .match() method. It returns true and the specified string.

I solved it now, the answer is /Oh{3,6} no/ . But I am curious as to why this is not working


#3

Well, why it is not working it should be obvious to you: almost the only thing you’ve changed ( flags apart) was the problem - o won’t match O.
Why it is working elsewhere, that would be an interesting question :stuck_out_tongue:


#4

If you are still facing the issue, try this

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


#5

Sorry, just learning Regex


#6

Just for anyone reading this solution. You have to add the \s tag to include whitespace at the end of your upper and lower number specification /Oh{3,6}\sno/ for it to work. The code presented as solution does not work!

The reason you have to put a capital O is that the test cases have capital O’s, You could alternatively write /oh{3,6}\sno/i.

I think the problem is with the g tag. But I have not yet figured out why this is causing an error. It works fine with g tag here for example.


#7

I’m still confused. My first attempt was:

let ohRegex = /h{3,6} /;

and it matched all points except Ohhhhhhh no - so it returned false for less than 3h but not for more than 6h.

it worked than with:

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

without the suggested \s

But nevertheless, I don’t understand why it accepts everything but a string with more than 6h.


#8

This is true ^^ That expression will match any statement into which it can find at least 3h and 6h at most. If you have a word with 7h that word contains a ‘cluster’ with 6h and this is the match^^

This regex will not match the 7h word because now the 3/6 h match must be followed by a space, not by another h^^

I think the problem is with the g tag. But I have not yet figured out why this is causing an error

I think that the problem could have something to do with the order of the tests, but I’m not sure about that.
Here is the reference that arouse my doubts: MDN - RegExp-prototype.test


#9

if you want to understand it more pricise ,use it with match wich retuns an array to see that we put a range like this {3,6} the match method takes only the letters that are between that range so that in test it does not seem very clear .


#10

Thx very much this helped @mustaphason and @Layer


#11

Hello, after many attempts i later got it. The White space in the ohStr value is what you have to add after the curly braces instead of “no”. So your syntax will go like /Oh{3,6} /; take note of the white space.


#12

Hello guys, I got the answer. We want to have “h” from 3 to 6. So we can go for the pattern: /[^h]h{3,6}[^h]/


#13

i used
let ohRegex = /Oh{3,6} no/;

and it worked


#14

ohRegex = /Oh{3,6} no/;
Mind an empty space between closing curly braces and no part. Since this is how’s in string itself …


#15

ohRegex = /oh{3,6}\sno/i; worked for me!


#16

I tried your answer and it was correct!Thanks!:grinning:


#17

The trick is no g-tag uppercase Oh{#,#}(space) no.
It needs the ‘no’ to know when to end


#18

Hi all,

I was using this and it was failing:
/Oh{3,6}\s/g

This passed in every other regex online but not in here.
It was complaining about not matching “Ohhhh” and “Ohhhhhh” which is not the case!
It worked after I removed the “g” flag.
Please, someone fix this question :frowning:


#19

I was getting extremely hung up on the same idea that /h{3,6}/ should work.

I had to think this through for a minute. Approaching it asking myself what the .test() method would return on a string with more than 6 ‘h’ helped me see why it is not the full solution.

However, the instructions and requirements for this challenge are quite vague and ambiguous to me.