Applying global matching of regular expressions can match the number of occurrences of a character, compare these times, and save and return the largest one. The code is as follows:
var countMost = function(str){
if(!str) return;
var _count = 0, _temp = 0, _reg, _char;
for(var i=0; i_reg = new RegExp(str.charAt(i), 'g');
_temp = str.match(_reg).length;
if(_temp > _count){
_count = _temp;
_char = str.charAt(i);
}
}
return _count;
//return {count:_count, char:_char};
};
The commented out code can also return the characters that appear the most times.