题解 | #子字符串频次#
子字符串频次
https://www.nowcoder.com/practice/053caf9d4d9d449bb45a6b02a572f71b
看了一下大家的题解都是暴力indexof,或者split,我没想到,居然用了正则表达式,也算完成了,有兴趣的可以看看
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"></head><body>
<script>
const _searchStrIndexOf = (str, target) => {
// 补全代码
/*
请补全JavaScript代码,该函数接受两个参数分别为字符串、子字符串,要求返回子字符串在字符串中出现的频次。
*/
let reg=new RegExp(target,'g')
let length=0
let comp
while(comp=reg.exec(str)!==null)
{
length++
}
return length
}
</script>
</body>
</html>
#exec#