当前位置:网站首页>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
边栏推荐
- Several key points in divorce agreement
- LeetCode——226. 翻轉二叉樹(BFS)
- MySql 导出数据库中的全部表索引
- 物联网协议的王者:MQTT
- SIGIR 2022 | University of Hong Kong and others proposed the application of hypergraph comparative learning in Recommendation System
- Detailed explanation of asymmetric cryptosystem
- Introduction to distributed cache / cache cluster
- 牛客网:设计LRU缓存结构 设计LFU缓存结构
- 数据加密标准(DES)概念及工作原理
- RSA encryption and decryption details
猜你喜欢
随机推荐
halcon之区域:多种区域(Region)特征(5)
如何将应用加入到deviceidle 白名单?
Digital signature standard (DSS)
How to open a stock account? Is it safe to open an account online now?
Secrets of gear contract
Decision tree and random forest
Redis and database data consistency
LeetCode——226. 翻转二叉树(BFS)
Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
DoS及攻擊方法詳解
How does Guosen Securities open an account? Is it safe to open a stock account through the link
The difference between round and truncate in SQL (round or truncate)
手写promise.all
[dynamic planning] Jianzhi offer II 091 Paint the house
RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)
【QNX】命令
How to add an application to the deviceidle whitelist?
丰富专业化产品线, 江铃福特领睿·极境版上市
贝叶斯网络详解
sparksql如何通过日期返回具体周几-dayofweek函数









