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

1.利用正则
function strCount(origin, str) {
var reg = new RegExp(str, 'g');
//console.log(origin.match(reg)); 调试查看 这里是个数组 其中存放了与它找到的匹配文本有关的信息
return origin.match(reg).length;
}
2.利用字符串拆分数组,拆分数组后的长度-1就是子字符串出现的次数
function strCount2(origin, str) {
return origin.split(str).length - 1;
}
版权声明:
作者:东明兄
链接:https://blog.crazyming.com/note/practice/1651/
来源:CrazyMing
文章版权归作者所有,未经允许请勿转载。
THE END
二维码
海报
统计某一字符或字符串在另一个字符串中出现的次数
统计某一字符或字符串在另一个字符串中出现的次数


共有 0 条评论