当前位置:网站首页>Groups and ranges of regular series
Groups and ranges of regular series
2022-07-01 22:58:00 【Old__ L】
Groups and ranges represent groups and ranges of expression characters .
1、x|y
matching "x" or "y" Any character .
let reg = /red|green/g;
let str = "green apple";
str.match(reg); // ['green']
str = "red apple";
str.match(reg); // ['red']
2、[xyz][a-c]
Character set . Match any contained character . You can use hyphens to specify character ranges , But if the hyphen appears as the first or last character in square brackets , Then it will be regarded as the text hyphen contained in the character set as ordinary characters . You can also include character classes in the character set .
let reg = /[abcd]/g;
let str = "green apple";
str.match(reg); // ['a']
reg = /[a-d]/g;
str.match(reg); // ['a']
3、[^xyz][^a-c]
A negative or supplemented character set . in other words , It matches any character that is not contained in parentheses . You can specify the character range by using hyphens , But if the hyphen appears as the first or last character in square brackets , Then it will be considered to be included in the character set as ordinary characters .
let reg = /[^abcd]/g;
let str = "green apple";
str.match(reg); // ['g', 'r', 'e', 'e', 'n', ' ', 'p', 'p', 'l', 'e']
reg = /[^a-d]/g;
str.match(reg); // ['g', 'r', 'e', 'e', 'n', ' ', 'p', 'p', 'l', 'e']
4、(x)
Capture group : matching x And remember the match .
A regular expression can have multiple capture groups , In the result , The matching capture group is usually in an array , Its members are in the same order as the left parentheses of the capture Group . This is usually just the order of the capture group itself . When capture groups are nested , This is very important . Use the index of the result element ([1], …, [n]) Or from predefined RegExp Object properties ($1, …, $9).
Capturing a group results in performance loss . If you don't need to recall the matching substring , Please select non captured parentheses .
let reg = /First_Name: (\w+), Last_Name: (\w+)/gm;
let str = `First_Name: John, Last_Name: Doe First_Name: Jane, Last_Name: Smith`;
let match = reg.exec(str);
match[1]; // John
match[2]; // Doe
5、(?:x)
Non capturing group : matching “x”, But I don't remember the match . The matching string cannot be extracted from the result array ( ([1], …, [n])) Or predefined RegExp Object properties ($1, …, $9) Found in .
let reg = /First_Name: (?:\w+), Last_Name: (?:\w+)/gm;
let str = `First_Name: John, Last_Name: Doe First_Name: Jane, Last_Name: Smith`;
let match = reg.exec(str);
match[1]; // John
match[2]; // Doe
reg.test(str); // true
6、(?<Name>x)
Named capture group : matching "x" And store it in the returned matching groups Attribute in < name > Under the specified name . Angle brackets (< and >) Is required for the group name .
let reg = /First_Name: (?<firstname>\w+), Last_Name: (?<lastname>\w+)/gm;
let str = `First_Name: John, Last_Name: Doe First_Name: Jane, Last_Name: Smith`;
let match = reg.exec(str);
match.groups.firstname; // John
match.groups.lastname; // Doe
7、\n
among n Is a positive integer . For regular expressions and n The reverse reference of the last substring matched by parentheses ( Calculate the left parenthesis ).
let me put it another way : Parentheses in regular expressions "()", It means grouping . If it comes after it \1 It is the same as the content to be matched in the first parenthesis .
let reg = /apple(,)\sorange\1/g;
let str = "apple, orange, cherry, peach";
str.match(reg); // ['apple, orange,']
8、\k<Name>
Yes <Name> The specified named capture group matches the back reference of the last substring .
Use it literally here \k To indicate the beginning of a backreference to a named capture Group .
let reg = /(?<title>\w+), yes \k<title>/g;
let str = "Do you copy? Sir, yes Sir!";
str.match(reg); // ['Sir, yes Sir']
Postscript
If you feel the article is not good
//(ㄒoㄒ)//, Just leave a message in the comments , The author continues to improve ;o_O???
If you think the article is a little useful , You can praise the author ;\\*^o^*//
If you want to progress with the author , Sure Wechat scan QR code , Focus on the front-end old L;~~~///(^v^)\\\~~~
Thank you readers(^_^)∠※!!!

边栏推荐
- vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
- "Trust machine" empowers development
- [image segmentation] 2021 segformer neurips
- 好友新书发布,祝贺(送福利)
- MySQL view exercise
- Niuke monthly race - logarithmic sum in groups
- Understanding of indexes in MySQL
- SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
- 半监督学习之数据增强
- 人体姿态估计的热图变成坐标点的两种方案
猜你喜欢

Cisco exam -- redundant network

转载csdn文章操作

Mysql5.7 set password policy (etc. three-level password transformation)
![[untitled]](/img/60/9a56e8b00c386779be13308515b24f.png)
[untitled]

Kubernetes create service access pod

Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock

软件测试之「 性能测试」总结,新手上路必会知识点

SAP 智能机器人流程自动化(iRPA)解决方案分享

Appium automated testing foundation - Supplement: introduction to desired capabilities parameters

Introduction and use of plantuml
随机推荐
[JUC learning road day 8] condition
Pytorch's code for visualizing feature maps after training its own network
cvpr2022 human pose estiamtion
[literacy] deep / shallow, local / global features in machine learning image processing
Explain the use of locksupport in detail
[daily training] 66 add one-tenth
"Trust machine" empowers development
104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
死锁的处理策略—预防死锁、避免死锁、检测和解除死锁
el-input文本域字数限制,超过显示变红并禁止输入
Mixconv code
Sogou wechat app reverse (II) so layer
Flynk SQL client uses comparison and is familiar with official documents
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
有些能力,是工作中学不来的,看看这篇超过90%同行
nn.Parameter】Pytorch特征融合自适应权重设置(可学习权重使用)
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
Turn -- the underlying debugging principle of GDB is so simple
I graduated from college in 14 years and changed to software testing in 3 months. My monthly salary was 13.5k. At the age of 32, I finally found the right direction
软件测试之「 性能测试」总结,新手上路必会知识点