var mainStr="star,star,study,study,star,hello";
var subStr="star";
function countStar(mainStr, subStr)
{
var count = 0;
var find = 0;
do
{
find = mainStr.indexOf(subStr, find);
if(find != -1)
{
count++;
find += subStr.length;
}
}while(find != -1)
return count;
}
document.write(countStar(mainStr, subStr));