当前位置:网站首页>Regular match same character
Regular match same character
2022-06-26 17:55:00 【Dandan's servant】
In many interview questions, I have seen some operations on the same character in a string , Most people will use the traversal method to get the same characters , But regularization can also meet some of our requirements .
grammar
()\n
Matching the same characters requires \ This metacharacter , And this metacharacter must match () Use .
give an example
Find the same two numbers
'1122'.match(/(\d)\1/g) //["11", "22"]
Deduce
Sometimes we need more than two identical numbers , We can continue in the future \1 operation
Find the same three characters
'112aadddddw2'.match(/(.)\1\1/g) //["ddd"]
You may not understand the number of the same characters , Actually, we can take it apart ,() Match once ,\1 Match once , Two \1 Just twice , Three times in all . Of course , It must be quite troublesome to match multiple times , So you can use {} Conduct \1 Replacement .
'112aadddddw2'.match(/(.)\1{2}/g) //["ddd"]
stay \1 Use... In the back {} That is to say \1 The number of .
Be careful :
Some people will certainly ask since \1 Indicates the previous () What's in it , that \2 Well , perhaps \n Well
Actually ,\1 Reference is made to the 1 Sub expression ,\2 Reference is made to the 2 Sub expression .
// Find a two digit number followed by a three letter character
/(\d)\1(\w)\2{2}/.test('x222aaas') // true
// Find a number repeated twice followed by a letter and finally followed by a letter
/(\d(\w))\1\2/.test('2w2ww') // true
边栏推荐
- Applet setting button sharing function
- 类型多样的石膏PBR多通道贴图素材,速来收藏!
- Niuke network: Design LRU cache structure design LFU cache structure
- [qt learning notes]qt inter thread data communication and data sharing
- #25class的类继承
- 无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!
- Tencent qianzhiming: Exploration and application of pre training methods in information flow business
- 9、智慧交通项目(2)
- #26class中get和set设置
- 行锁分析和死锁
猜你喜欢

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

Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!

next(iter(dataloader))的一点点体会

wechat_微信小程序中解决navigator进行页面跳转并传递参数问题

How pycharm modifies multiline annotation shortcuts

小程序设置按钮分享功能

Redis and database data consistency

Plt How to keep show() not closed

RSA加密解密详解

数据加密标准DES安全性
随机推荐
正则匹配相同字符
决策树与随机森林
Analysis of deep security definition and encryption technology
接水面试题
RSA概念详解及工具推荐大全 - lmn
陈强:阿里千亿级大规模数字商业知识图谱助力业务增长
国信证券怎么开户?通过链接办理股票开户安全吗
[recommendation system learning] recommendation system architecture
Lm06 the mystery of constructing the bottom and top trading strategy only by trading volume
行锁与隔离级别案例分析
I want to know. I am in Zhaoqing. Where can I open an account? Is it safe to open an account online?
[buuctf.reverse] 126-130
有依赖的背包问题
数据加密标准(DES)概念及工作原理
背包问题求方案数
牛客网:设计LRU缓存结构 设计LFU缓存结构
How pycharm modifies multiline annotation shortcuts
Introduction to distributed cache / cache cluster
A little experience of next (ITER (dataloader))
ZCMU--1367: Data Structure