当前位置:网站首页>正则匹配相同字符
正则匹配相同字符
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 - 1 The era of net developers
- Here comes the hero League full skin Downloader
- 【动态规划】剑指 Offer II 091. 粉刷房子
- vue--vuerouter缓存路由组件
- Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
- 关于FlowUs这一款国民好笔记
- Basic requirements: 7 problems in singleton mode
- Platform management background and merchant menu resource management: access control design of platform management background
- MySQL index
- Concurrent thread safety
猜你喜欢

Community ownership of NFT trading market is unstoppable

Wechat app mall, review products, upload commodity pictures, and score Commodity Services

Vue--vuerouter cache routing component

Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class

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

When I was in the library, I thought of the yuan sharing mode

Byte interview: two array interview questions, please accept

Redis' 43 serial cannons, try how many you can carry

Microservice architecture practice: business management background and SSO design: SSO design

Rich professional product lines, and Jiangling Ford Lingrui · Jijing version is listed
随机推荐
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)
Discussion: the next generation of stable coins
The texstudio official website cannot be opened
Redis and database data consistency
Introduction to distributed cache / cache cluster
14 MySQL tutorial insert insert data
类型多样的石膏PBR多通道贴图素材,速来收藏!
Detailed contract quantification system development scheme and technical description of quantitative contract system development
Teach you to learn dapr - 2 Must know concept
[Error] ld returned 1 exit status
[latex bearer] use tables in \title (error \begin doesn't match its definition.)
The high concurrency system is easy to play, and Alibaba's new 100 million level concurrent design quick notes are really fragrant
【Unity】在Unity中使用C#执行外部文件,如.exe或者.bat
Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
Community ownership of NFT trading market is unstoppable
Introduction to minimal API
The difference between round and truncate in SQL (round or truncate)
合约量化系统开发方案详细,量化合约系统开发技术说明
[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers
[C language] static modifies local variables