I think you are complicating things with what this should be. Use regex. It is also case insensitive. If you need a register with a register, delete me after g.
var str = "This is some text";
var pattern = /t/gi;
alert(str.match(pattern).length);
Make it shorter.
var str = "This is some text";
alert(str.match(/t/gi).length);
user356808
source
share