当前位置:网站首页>正则系列之组和范围(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^)\\\~~~
谢谢各位读者们啦(^_^)∠※!!!

边栏推荐
- [C language] detailed explanation of malloc function [easy to understand]
- Fiori applications are shared through the enhancement of adaptation project
- Configure filter
- Favorite transaction code management tool in SAP GUI
- 好友新书发布,祝贺(送福利)
- redis配置文件中常用配置详解[通俗易懂]
- Mysql5.7 set password policy (etc. three-level password transformation)
- C#/VB. Net to add text / image watermarks to PDF documents
- Using emqx cloud to realize one machine one secret verification of IOT devices
- 内部字段分隔符
猜你喜欢

台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条

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

104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目

C#/VB. Net to add text / image watermarks to PDF documents

每日刷题记录 (十)

How to write a performance test plan

SAP intelligent robot process automation (IRPA) solution sharing

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

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

# CutefishOS系统~
随机推荐
Use and function of spark analyze command map join broadcast join
Origin2018 installation tutorial "recommended collection"
MySQL5.7 设置密码策略(等保三级密码改造)
[image segmentation] 2021 segformer neurips
Easyexcel complex data export
The principle, testing and Countermeasures of malicious software reverse closing EDR
The fixed assets management subsystem reports are divided into what categories and which accounts are included
Selection of all-optical technology in the park - Part 2
每日刷题记录 (十)
Kubernetes create service access pod
多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
Flink SQL command line connection yarn
Using emqx cloud to realize one machine one secret verification of IOT devices
Intelligent computing architecture design of Internet
Wechat open platform scanning code login [easy to understand]
There is no signal in HDMI in computer games caused by memory, so it crashes
Sogou wechat app reverse (II) so layer
Appium automation test foundation - appium installation (I)
Cloud Vulnerability Global Database
“信任机器”为发展赋能