You could remove any other character in the string and check the length:
str.replace(/[^a]/g, "").length
Here it is counted how many a
s are in str
. The RegExp
is described below:
[ // Start Character Group
^ // Not operator in character group
a // The character "a"
] // End character group