使用indexOf()算出长字符串中包含多少个短字符串

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

posted @ 2014-03-10 12:14  白小呆阿武哒  阅读(182)  评论(0)    收藏  举报