当前位置:网站首页>正则表达式小示例--获取重复最多的字符及其数量
正则表达式小示例--获取重复最多的字符及其数量
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 + " 个");
捕获组的概念:正确使用正则表达式的捕获组和非捕获组
边栏推荐
- ACL 和NAT
- 线上问题排查流程
- Call the TensorFlow Objection Detection API for object detection and save the detection results locally
- UE5再次更新!扫描或手动建模面部模型可直接转为绑定好的Metahuman
- Remembering my first CCF-A conference paper | After six rejections, my paper is finally accepted, yay!
- 静态路由
- UE4动画雨滴材质制作教程
- Blender软件介绍与使用心得
- Hard Disk Partitioning and Permanent Mounting
- I/O性能与可靠性
猜你喜欢
【机器学习】1单变量线性回归
【Day8】Knowledge about disk and disk partition
The problem of calling ds18b20 through a single bus
腾讯内部技术:《轩辕传奇》服务器架构演变
Remembering my first CCF-A conference paper | After six rejections, my paper is finally accepted, yay!
Blender软件介绍与使用心得
阿里云视频点播
TCP/IP four-layer model
如何用UE5渲染一个可爱的茶壶屋?
什么?CDN缓存加速只适用于加速静态内容?
随机推荐
spark source code-RPC communication mechanism
ACL 和NAT
洞察互联网大趋势,读完这篇文章你就彻底了解中文域名
VLAN详解及实验
I217-V network disconnection problem in large traffic under openwrt soft routing
ACLs and NATs
[Day5] Soft and hard links File storage, deletion, directory management commands
入门文档10 资源映射
IP地址及子网的划分
ROS视频教程
NIO工作方式浅析
传输层协议(TCP3次握手)
VRRP principle and command
spark source code - task submission process - 2-YarnClusterApplication
调用TensorFlow Objection Detection API进行目标检测并将检测结果保存至本地
D46_给刚体施加的力
The spark operator - repartition operator
【Day8】Knowledge about disk and disk partition
spark source code - task submission process - 1-sparkSubmit
Account and Permission Management