zivenday / learning

知识点脑图,js实现,算法实现。
MIT License
1 stars 1 forks source link

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

Open zivenday opened 5 years ago

zivenday commented 5 years ago

https://github.com/haizlin/fe-interview/issues/21

zivenday commented 5 years ago

https://github.com/haizlin/fe-interview/issues/21

zivenday commented 5 years ago
    function countStr(str, target) {
        if (!str || target === null || target === undefined) return 0
        let reg = new RegExp(`(${target})`, 'g')
        var count = 0
        str.replace(reg, function(match, c) {
            ++count
            return c
        })
        console.log(count)
    }
    countStr('assssssa', 'b')