当前位置:网站首页>正则系列之组和范围(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^)\\\~~~
谢谢各位读者们啦(^_^)∠※
!!!
边栏推荐
- Detailed explanation of common configurations in redis configuration file [easy to understand]
- 阿洛迷茫后的思考
- Use and function of spark analyze command map join broadcast join
- 今日睡眠质量记录71分
- Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
- Rust语言——小小白的入门学习05
- 恶意软件反向关闭EDR的原理、测试和反制思考
- [C language] detailed explanation of malloc function [easy to understand]
- el-input文本域字数限制,超过显示变红并禁止输入
- 使用 EMQX Cloud 实现物联网设备一机一密验证
猜你喜欢
Redis configuration and optimization
SAP intelligent robot process automation (IRPA) solution sharing
SAP 智能机器人流程自动化(iRPA)解决方案分享
转--深入LUA脚本语言,让你彻底明白调试原理
FFMpeg学习笔记
内部字段分隔符
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
转--原来gdb的底层调试原理这么简单
Easyexcel complex data export
Slope compensation
随机推荐
twenty million two hundred and twenty thousand seven hundred and one
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
104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
General use of qstringlist
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
MySQL stored procedure
Redis configuration and optimization
旅游管理系统
# CutefishOS系统~
ECMAScript 2022 正式发布,有你了解过的吗?
Preparation of functional test report
Efficiency improvement - encourage personalized container development environment
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines
Kubernetes创建Service访问Pod
[C language] detailed explanation of malloc function [easy to understand]
H5 model trained by keras to tflite
Tourism Management System
MySQL view exercise
Metauniverse may become a new direction of Internet development
SAP intelligent robot process automation (IRPA) solution sharing