function countInstance(mainStr,srchStr){
var count = 0;
var offset =
0;// 搜索的起始位置
do{
offset = mainStr.indexOf(srchStr,offset);
count
+= (offset != -1)? 1:0;
}while(offset++ != -1);
return
count;
}
alert(countInstance("wwweeewwwweee",‘we‘)); //2
使用indexOf()算出长字符串中包含多少个短字符串,布布扣,bubuko.com
原文:http://www.cnblogs.com/xiaowuge/p/3591512.html