当前位置:网站首页>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(^_^)∠※!!!

边栏推荐
- Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
- Deep learning -- data operation
- Detailed explanation of common configurations in redis configuration file [easy to understand]
- 有些能力,是工作中学不来的,看看这篇超过90%同行
- Sogou wechat app reverse (II) so layer
- vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
- El input text field word limit, beyond which the display turns red and input is prohibited
- Intelligent computing architecture design of Internet
- 使用 EMQX Cloud 实现物联网设备一机一密验证
- ECMAScript 2022 正式发布,有你了解过的吗?
猜你喜欢

每日刷题记录 (十)

思科考试--冗余网络

SAP GUI 里的收藏夹事务码管理工具

激发新动能 多地发力数字经济

104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code

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

internal field separator

“信任机器”为发展赋能

Understanding of indexes in MySQL

Hide the creation and use of users
随机推荐
MySQL stored procedure
nn. Parameter] pytoch feature fusion adaptive weight setting (learnable weight use)
[daily training] 66 add one-tenth
The principle, testing and Countermeasures of malicious software reverse closing EDR
Happy number [fast and slow pointer of ring PROBLEMS]
Daily question brushing record (10)
使用 EMQX Cloud 实现物联网设备一机一密验证
轉載csdn文章操作
Réimpression de l'article csdn
陈天奇的机器学习编译课(免费)
vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
map容器
倒置残差的理解
Cloud Vulnerability Global Database
[target tracking] | single target tracking indicator
Cutefishos system~
Electron学习(三)之简单交互操作
Resttemplate remote call tool class
有些能力,是工作中学不来的,看看这篇超过90%同行
Chen Tianqi's machine learning compilation course (free)