当前位置:网站首页>正则系列之组和范围(Groups and Ranges)
正则系列之组和范围(Groups and Ranges)
2022-07-01 21:51:00 【老__L】
组和范围表示表达式字符的组和范围。
1、x|y
匹配 "x" 或 "y" 任意一个字符。
let reg = /red|green/g;
let str = "green apple";
str.match(reg); // ['green']
str = "red apple";
str.match(reg); // ['red']
2、[xyz][a-c]
字符集。匹配任何一个包含的字符。您可以使用连字符来指定字符范围,但如果连字符显示为方括号中的第一个或最后一个字符,则它将被视为作为普通字符包含在字符集中的文字连字符。也可以在字符集中包含字符类。
let reg = /[abcd]/g;
let str = "green apple";
str.match(reg); // ['a']
reg = /[a-d]/g;
str.match(reg); // ['a']
3、[^xyz][^a-c]
一个否定的或被补充的字符集。也就是说,它匹配任何没有包含在括号中的字符。可以通过使用连字符来指定字符范围,但是如果连字符作为方括号中的第一个或最后一个字符出现,那么它将被视为作为普通字符包含在字符集中。
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)
捕获组:匹配 x 并记住匹配项。
一个正则表达式可以有多个捕获组,在结果中,匹配的捕获组通常在一个数组中,其成员和捕获组的左括号的顺序一样。这通常只是捕获组本身的顺序。当捕获组被嵌套时,这一点非常重要。使用结果元素的索引 ([1], …, [n]) 或从预定义的 RegExp 对象的属性 ($1, …, $9)。
捕获组会带来性能损失。如果不需要收回匹配的子字符串,请选择非捕获括号。
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)
非捕获组:匹配 “x”,但不记得匹配。匹配的字串不能从结果数组( ([1], …, [n]))或者预定义的 RegExp 对象的属性 ($1, …, $9)中被找回。
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)
具名捕获组:匹配"x"并将其存储在返回匹配的groups属性中在< 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
其中 n 是一个正整数。对正则表达式中与 n 括号匹配的最后一个子字符串的反向引用 (计算左括号)。
换句话说:正则表达式中的小括号"()",是代表分组的意思。 如果再其后面出现\1则是代表与第一个小括号中要匹配的内容相同。
let reg = /apple(,)\sorange\1/g;
let str = "apple, orange, cherry, peach";
str.match(reg); // ['apple, orange,']
8、\k<Name>
对<Name>指定的具名捕获组匹配的最后一个子串的反向引用。
这里按字面意思使用\k来指示对具名捕获组的反向引用的开始。
let reg = /(?<title>\w+), yes \k<title>/g;
let str = "Do you copy? Sir, yes Sir!";
str.match(reg); // ['Sir, yes Sir']
后记
如果你感觉文章不咋地
//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???
如果你觉得该文章有一点点用处,可以给作者点个赞;\\*^o^*//
如果你想要和作者一起进步,可以微信扫描二维码,关注前端老L;~~~///(^v^)\\\~~~
谢谢各位读者们啦(^_^)∠※!!!

边栏推荐
- MySQL MHA high availability configuration and failover
- vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
- Intelligent computing architecture design of Internet
- QStringList 的常规使用
- Réimpression de l'article csdn
- Origin2018 installation tutorial "recommended collection"
- Understanding of indexes in MySQL
- How to write a performance test plan
- SAP 智能机器人流程自动化(iRPA)解决方案分享
- Rust language - Introduction to Xiaobai 05
猜你喜欢

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

Use three JS realize the 'ice cream' earth, and let the earth cool for a summer

# CutefishOS系统~

2020-ViT ICLR

Fully annotated SSM framework construction

Favorite transaction code management tool in SAP GUI

3DE resources have nothing or nothing wrong

元宇宙可能成为互联网发展的新方向

Learn MySQL from scratch - database and data table operations

Appium自动化测试基础 — APPium安装(一)
随机推荐
转--深入LUA脚本语言,让你彻底明白调试原理
Detailed explanation of common configurations in redis configuration file [easy to understand]
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
internal field separator
Flynk SQL client uses comparison and is familiar with official documents
LC501. Mode in binary search tree
使用 EMQX Cloud 实现物联网设备一机一密验证
SAP intelligent robot process automation (IRPA) solution sharing
Slope compensation
Rust language - Introduction to Xiaobai 05
ECMAScript 2022 正式发布,有你了解过的吗?
[daily training] 326 Power of 3
Yolov5.5 call local camera
Today's sleep quality record 71 points
Measurement of reference loop gain and phase margin
mixconv代码
激发新动能 多地发力数字经济
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
Turn -- the underlying debugging principle of GDB is so simple
Cloud Vulnerability Global Database