I would like to check if f.e letter "a" occurs between 3 and 5 times (inclusive), in total, within provided text input.
Given:
zayaaxaawv
<- 5 of "a", passedzayaxawva
<- 4 of "a", passedzaaay
<- 3 of "a", passedzayax
<- 2 of "a", failedzayaaxaaaw
<- 6 of "a", failed
Examples 1-3 passed and 4-5 failed.
I know it can be done in JavaScript without using RegExp
, f.e:
const txt = `zayaxawva`;
const charCountByFilter = [...txt].filter(letter => letter === 'a').length;
const charCountByMatch = txt.match(/a/g).length;
console.log('isInRange by filter', isInRange(charCountByFilter));
console.log('isInRange by match', isInRange(charCountByMatch));
function isInRange(value) {
return value >= 3 && value <= 5;
}
However, is it possible to do it entirely by RegExp? I thought of something similar to/a{3,5}/.test(txt)
, but it only counts subsequent "a" chars, not all of them.