使用此函數能返回字符串中某個字符或字符串出現的次數其中mainStr為要查找的字符串,subStr為要檢查的字符串或字符
function countInstances(mainStr, subStr)
{
var count = 0;
var offset = 0;
do
{
offset = mainStr.indexOf(subStr, offset);
if(offset != -1)
{
count++;
offset += subStr.length;
}
}while(offset != -1)
return count;
}