当前位置:网站首页>正则匹配相同字符
正则匹配相同字符
2022-06-26 17:23:00 【丹丹的小跟班】
我在很多面试题里都看到有在一个字符串里对相同字符的一些操作,获取相同字符大多人都会用遍历的方法,但是正则也是可以满足我们部分需求的。
语法
()\n
匹配相同字符需要用到\这个元字符,而这个元字符又必须配合()使用。
举例
求相同两个的数字
'1122'.match(/(\d)\1/g) //["11", "22"]
推演
有时候我们需要的不仅仅是两个相同数字,我们可以继续在后面继续\1操作
求相同三个的字符
'112aadddddw2'.match(/(.)\1\1/g) //["ddd"]
大家可能对相同字符的数量判断有些不太明白,其实我们可以拆开看,()匹配一次,\1匹配一次,两个\1就是两次,总共三次。当然,若是要进行多次匹配肯定相当麻烦,所以可以使用{}进行\1的替换。
'112aadddddw2'.match(/(.)\1{2}/g) //["ddd"]
在\1后面使用{}就是表示\1的数量。
注意:
有人肯定会发问既然\1表示前一个()里面的内容,那么\2呢,或者\n呢
其实呢,\1引用的是第 1 个子表达式,\2引用的是第 2 个子表达式。
//求一个两位数字后面跟着三个字母的字符
/(\d)\1(\w)\2{2}/.test('x222aaas') // true
//求重复两次的一个数字跟着一个字母并最后跟着一个字母
/(\d(\w))\1\2/.test('2w2ww') // true
边栏推荐
- js强制转换
- Cloud native 02: Alibaba cloud cloud efficient flow pipeline
- Technical scheme design of chain game system development - NFT chain game system development process and source code
- Decentralized NFT transaction protocol will defeat opensea
- Preparing for the Blue Bridge Cup and ccf-csp
- SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用
- Problems encountered this week
- Niuke network: Design LRU cache structure design LFU cache structure
- Can Luo Yonghao succeed in entering the AR field this time?
- 类型多样的石膏PBR多通道贴图素材,速来收藏!
猜你喜欢

How sparksql returns a specific day of the week by date -dayofweek function

Synchronized description of concurrency

Jouer avec Linux et installer et configurer MySQL facilement

Programmer's essential toolkit, please collect!

Distributed Architecture Overview

Teach you to learn dapr - 4 Service invocation
![[buuctf.reverse] 126-130](/img/df/e35633d85caeff1dece62a66cb7804.png)
[buuctf.reverse] 126-130

【代码随想录-动态规划】T583、两个字符串的删除操作

Interpretation of new plug-ins | how to enhance authentication capability with forward auth
![[C language] static modifies local variables](/img/bf/9084d2e924c3e1e244568562a83d74.jpg)
[C language] static modifies local variables
随机推荐
直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!
Basic requirements: 7 problems in singleton mode
离婚协议中的几个重点
Fire evacuation and self rescue... This safety production and fire training is full!
Teach you to learn dapr - 6 Publish subscription
20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)
分布式缓存/缓存集群简介
Microservice architecture practice: business management background and SSO design, SSO client design
Troubleshooting ideas that can solve 80% of faults!
[dynamic planning] Jianzhi offer II 091 Paint the house
The texstudio official website cannot be opened
Discussion: the next generation of stable coins
Microservice architecture practice: business management background and SSO design: SSO design
Implementation of MySQL master-slave architecture
Vue--vuerouter cache routing component
How about opening an account at Guojin securities? Is it safe?
The king of Internet of things protocol: mqtt
Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
Halcon's region: features of multiple regions (5)
In those years, interview the abused red and black trees