当前位置:网站首页>正则匹配相同字符
正则匹配相同字符
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
边栏推荐
- Troubleshooting ideas that can solve 80% of faults!
- Detailed explanation of browser storage methods: the origin and difference of cookies, localstorage and sessionstorage
- 【推荐系统学习】推荐系统架构
- Treasure and niche CTA animation material website sharing
- 9、智慧交通项目(2)
- 【代码随想录-动态规划】T583、两个字符串的删除操作
- 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)
- [buuctf.reverse] 126-130
- [code Capriccio - dynamic planning] t583. Deleting two strings
- halcon之区域:多种区域(Region)特征(5)
猜你喜欢

Niuke network: Design LRU cache structure design LFU cache structure

Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!

When I was in the library, I thought of the yuan sharing mode
![[suggested collection] 11 online communities suitable for programmers](/img/6b/d5c68e93384fd314d0cb27d9df1cb9.jpg)
[suggested collection] 11 online communities suitable for programmers

Vscode usage - Remote SSH configuration description
![[recommendation system learning] recommendation system architecture](/img/a8/448f6e708227555bb6b32cdc652435.png)
[recommendation system learning] recommendation system architecture

Incomplete line spacing adjustment of formula display in word

Romance of the Three Kingdoms: responsibility chain model

Teach you to learn dapr - 9 Observability

7 views on NFT market prospect
随机推荐
When I was in the library, I thought of the yuan sharing mode
Viewing the task arrangement ability of monorepo tool from turborepo
有依赖的背包问题
Ndroid development from introduction to mastery Chapter 2: view and ViewGroup
Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
The king of Internet of things protocol: mqtt
在国金证券开户怎么样?保障安全吗?
Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
Incomplete line spacing adjustment of formula display in word
LeetCode——226. Flip binary tree (BFS)
Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
Detailed explanation of browser storage methods: the origin and difference of cookies, localstorage and sessionstorage
二分查找-2
Platform management background and merchant menu resource management: merchant registration management design
Leetcode - 226. Retourner l'arbre binaire (bfs)
SQL injection for Web Security (3)
Leetcode daily [2022 - 02 - 16]
分布式缓存/缓存集群简介
Convert the decimal positive integer m into the number in the forward K (2 < =k < =9) system and output it in bits
Leetcode HOT100 (22--- bracket generation)