SOURCE

// 非常巧妙的一种方法
// function search(parent, child){
//     return parent.split(child).length - 1;
// }

// var res = search("abababTDFTDFtdf","ab")
// console.log(res)

function substrCount(str, target) {
if(Object.prototype.toString.call(str).slice(8,-1) === 'String' && !str)
    console.log("请填写字符串");
else
    return (str.match(new RegExp(target, 'g')).length);
}

var res = substrCount("abababTDFTDFtdf","ab")
console.log(res)
console 命令行工具 X clear

                    
>
console