Return substring instance count - 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function searchSubstr( fullText, searchText, allowOverlap ){
if(fullText == '' || searchText == '') return 0;
var regex = new RegExp(searchText, 'g'), t = 0, match;
while(match = regex.exec(fullText)) {
t++;
if(allowOverlap) regex.lastIndex = match.index+1;
}
return t;
}