Glitch in the 'Specify Upper and Lower Number of Matches' lesson?

I was wondering if the quantity specifiers were inclusive/exclusive (99.9% sure they are inclusive), so I was playing around with the sample code:

let A4 = "aaaah";
let multipleA = /a{3,5}h/;
let result = multipleA.test(A4);

I got result to equal false when I changed A4 = "aah"; but when adding a’s to A4, result stayed true even when I got to 6 a’s and beyond - for example:

let A4 = "aaaaaaaaah";
let multipleA = /a{3,5}h/;
let result = multipleA.test(A4);

This result still returned true. Was wondering if this is a glitch or I’m not understanding something? I thought the 5 in {3,5} meant a maximum of 5 repeating a’s to get a match.

It matches as long as a substring of the string correspond to the pattern, in aaaaaaaaaaah you have a substring with 3-to-5 a followed by h, so it tests true

1 Like

If you want to check whether a string contains at most a certain number of repetitions, you need to include what comes before and after.
For example, if we want “bah”, “baah”, and “baaah”, but nothing else we could do /ba{1,3}h/. There are special tokens for the beginning and ending of the string for when we want to specify nothing coming before and/or after.

1 Like

Ohhh no my brain!

So say we have 8 a’s aaaaaaaah

Is the test call finding its match in the last 5 a's followed by the h - basically bypassing the first 3 a's?

From what ArielLeslie said, I also tried changing the regex to /^a{3,5}h/ and was able to get return to equal false with A4 = "aaaaaaaah" I think because the ^ makes it so pattern of the minimum 3 a's and max 5 a's has to be the first part of the string directly followed by an h?

You got it!

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