js判断字符串出现的次数

2019-08-09 17:08:42 浏览数 (1)

// 判断substr字符串在str中出现的次数 isIgnore是否忽略大小写!

代码语言:javascript复制
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;
}

0 人点赞