(第七天)[js] 统计某一字符或字符串在另一个字符串中出现的次数

 统计某一字符或字符串在另一个字符串中出现的次数

  

function asdf(arr,d){
    let count  = 0;
    while(arr.match(d)){
        arr = arr.replace(d,'');
        count++;
    }
    console.log(count);
}

let arr = 'asdadasdgfhgfhad';
let d = 'ad';
asdf(arr,d);

补充下知识点,说不定就用到了呢,您说不是吗~

match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。

该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。
原文地址:https://www.cnblogs.com/DIVEY/p/15204553.html