验证中...
repetitiveCount
Raw Copy
// 找出a重复次数 分享一道Google面试题:”实现一个函数,它返回一个字符串中字母A出现的次数“...#掘金沸点#
https://juejin.im/pin/5de5b7b0518825590ba3de36
var str = "cjdadvdartaoa";
// 1.正则
var len = str.length - str.replace(/a/gi, "").length;
function getRepetitiveCount(str, repetitiveLetter) {
var reg = new RegExp(repetitiveLetter, "ig");
return str.length - str.replace(reg, "").length;
}
// 2. split
function getRepetitiveCountOther(str, repetitiveLetter) {
var arr = str.split(repetitiveLetter);
return arr.length - 1;
}
console.info(getRepetitiveCountOther(str, "a"));

Comment list( 0 )

Sign in for post a comment

Help Search