当前位置:网站首页>正则表达式小示例--获取重复最多的字符及其数量
正则表达式小示例--获取重复最多的字符及其数量
2022-08-05 05:19:00 【CrazyQiQi】
通过正则表达式,从一个随机的字符串中获取重复最多的额字符及其数量
//首先声明定义一个随机的字符串
var str = 'hgldsfkjhgkejhgkjg4654dfhghjf45gfgdrg4GJHGJK5dzfdf241b35sf';
//1.将每一个字符切割为数组
var arrStr = str.split('');
//2.对字符数据进行排序
arrStr = arrStr.sort();
//3.将排好序的字符数据组装成字符串
str = arrStr.join('');
var result = ''; //返回最多的字母
var count = 0; //计数器
//4.捕获组——正则表达式中表达式匹配的内容,
//保存到内存中以数字的编号或显式命名的组里,方便后面引用
var reg = /(\w)\1+/gi; //捕获组
//用replace来获取最多的字符数
str.replace(reg, function($0, $1) {
//集合:$0代表连接字符组成的字符串,$1代表连续字符串中第一个首字母
if (count < $0.length) {
result = $1;
count = $0.length;
}
})
console.log("字符最多的是: " + result + " ,有 " + count + " 个");
捕获组的概念:正确使用正则表达式的捕获组和非捕获组
边栏推荐
猜你喜欢
![[Pytorch study notes] 8. How to use WeightedRandomSampler (weight sampler) when the training category is unbalanced data](/img/29/5b44c016bd11f0c0a9110cf513f4e1.png)
[Pytorch study notes] 8. How to use WeightedRandomSampler (weight sampler) when the training category is unbalanced data

通过单总线调用ds18b20的问题

Unity huatuo 革命性热更系列1.3 huatuo示例项目源码分析与启发

Hard Disk Partitioning and Permanent Mounting

IJCAI 2022|Boundary-Guided Camouflage Object Detection Model BGNet

Cocos Creator开发中的事件响应

网络布线与数制转换

【机器学习】1单变量线性回归

unity实现第一人称漫游(保姆级教程)

Unity常用模块设计 : Unity游戏排行榜的制作与优化
随机推荐
Spark源码-任务提交流程之-6-sparkContext初始化
D39_欧拉角与四元数
I/O性能与可靠性
Getting Started 11 Automatically add version numbers
账号与权限管理
腾讯云云函数SCF—入门须知
[Day8] (Super detailed steps) Use LVM to expand capacity
[Pytorch study notes] 8. How to use WeightedRandomSampler (weight sampler) when the training category is unbalanced data
UE4动画雨滴材质制作教程
Unity物理引擎中的碰撞、角色控制器、Cloth组件(布料)、关节 Joint
spark算子-repartition算子
Lua,ILRuntime, HybridCLR(wolong)/huatuo热更对比分析
入门文档05-2 使用return指示当前任务已完成
Unity3D中的ref、out、Params三种参数的使用
CIPU,对云计算产业有什么影响
Dsf5.0 bounced points determine not return a value
Three modes of vim
spark operator-wholeTextFiles operator
Getting Started Document 07 Staged Output
spark source code - task submission process - 1-sparkSubmit