当前位置:网站首页>正则匹配相同字符
正则匹配相同字符
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
边栏推荐
- Teach you to learn dapr - 4 Service invocation
- Here comes the hero League full skin Downloader
- Introduction to distributed cache / cache cluster
- Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
- Prometeus 2.34.0 新特性
- [C language] static modifies local variables
- 【Unity】在Unity中使用C#执行外部文件,如.exe或者.bat
- 链游系统开发技术方案设计丨NFT链游系统开发流程及源码
- Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
- 玩轉Linux,輕松安裝配置MySQL
猜你喜欢

物联网协议的王者:MQTT

Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!

Leetcode - 226. Retourner l'arbre binaire (bfs)

丰富专业化产品线, 江铃福特领睿·极境版上市

Platform management background and merchant menu resource management: merchant registration management design
![[suggested collection] 11 online communities suitable for programmers](/img/6b/d5c68e93384fd314d0cb27d9df1cb9.jpg)
[suggested collection] 11 online communities suitable for programmers

VSCode使用 - Remote-SSH 配置说明

直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!

14《MySQL 教程》INSERT 插入数据

Synchronized description of concurrency
随机推荐
玩轉Linux,輕松安裝配置MySQL
[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure
类型多样的石膏PBR多通道贴图素材,速来收藏!
Niuke network: Design LRU cache structure design LFU cache structure
Byte interview: two array interview questions, please accept
MySQL index
Programmer interview guide - self introduction
有依赖的背包问题
mysql Add column 失败 因为之前有数据,不是默认null 不行
Microservice architecture practice: user login and account switching design, order query design of the mall
Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
Record the use process of fenics
【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
[recommendation system learning] technology stack of recommendation system
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)
Redis' 43 serial cannons, try how many you can carry
Demonstrate to Xiaobai the case of sub database and sub table
Army chat -- registration of Registration Center
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
量化合约系统开发分析案例丨合约量化系统开发方案详解