js判斷字符串出現的次數


// 判斷substr字符串在str中出現的次數 isIgnore是否忽略大小寫!

function countSubstr(str, substr, isIgnore) {
	var count;
	var reg = "";
	if (isIgnore == true) {
		reg = "/" + substr + "/gi";
	} else {
		reg = "/" + substr + "/g";
	}
	reg = eval(reg);
	if (str.match(reg) == null) {
		count = 0;
	} else {
		count = str.match(reg).length;
	}
	return count;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM