当前位置:网站首页>regular expression
regular expression
2022-07-03 07:51:00 【Peter Sue】
Regular expressions (Regular Expression)
1) Regular creation
2) Regular matching method
3) Metacharacters
4) Character set
5) The border
6) grouping
7) backreferences
8) Quantitative vocabulary
9) Matching mode
// Regular expressions ;["12312","2312"]
// Find the corresponding number in the string ;
Regular implementation ; + appear 1 Times to times ; \d; Numbers ;\D: The digital ;
let reg = /\d+/g;
let arr = str.match(reg);
console.log(arr);
Knowledge point
### String manipulation
- Find the number in the string ;
- How to implement regularization
Regular creation
- Literal creation
- By constructor
// Regular create ;
// Mode one : Literal creation ;
let str = "fdsadf12313fdaf";
let a = "ad";
let reg = /a/;
let res = str.match(reg);
console.log(res);
// Creation mode 2 : Constructor creation ;
// let str = "fdsadf12313fdaf";
// let a = "ad";
// let reg = new RegExp(a,"g");
// let res = str.match(reg);
// console.log(res);
Regular matching method
String method
- match
- search
- replace
- split
1.split
let str = "fdafdafdsfdsafsddarer";
let arr = str.split(/a/);
console.log(arr);
//2.replace;
let str = "fdafdaf1dsfdsa2fsdda33rer";
let reg = /\d+/g;
let res = str.replace(reg,"*");
console.log(res); fdafdaf*dsfdsa*fsdda*rer
// Sensitive word filtering ;
let str = " According to the data of China's Ministry of Education ,2007 - 2018 year , The number of Chinese studying abroad is from 14 Ten thousand to 66.2 ten thousand , The average annual growth is about 14%; The number of returned students in the same period increased from 4 Ten thousand to 51.9 ten thousand , Average annual growth of nearly 24%. Under the rush of returning home , How about the supply and demand market for returnees , Become the common concern of overseas students and domestic employers .";
// The Ministry of Education 、 personnel ;
let reg = / The Ministry of Education | personnel /g;
// let res = str.replace(reg,"*");
let res = str.replace(reg,function(arg){
return "*".repeat(arg.length);
})
console.log(res);
// 3.match;
let str = "fdsa1231dfaf232fda";
let reg = /\d+/g;
let res = str.match(reg);
console.log(res);
// matching // Content between
let str = /official/bannerManage
let reg = /\/([\s\S]*?)\//
str = str.match(reg)[1]
console.log(str, ' result ')//official
// 4.search: Match the position of the first index value that matches the result ; If you can't find it Just return it -1;
// Ignore all matches ;
let str = "fdsfda323fdaf1232";
let reg = /\d+/;
let res = str.search(reg);
console.log(res);
Methods under regular objects
- test
- exec
// The regular way ;
// 1.test 2.exce;
let str = "dfasfd342234fda2342342f";
let reg = /\d+/;
let res = reg.test(str);
console.log(res); //true
let reg = /\d+/g;
let res = reg.exec(str);
console.log(res);
let res1 = reg.exec(str);
console.log(res1);
Metacharacters
. *(0 Times or more ) + ? $ ^ | \ () [] {
};
- Non alphabetic characters with special meaning in regular expressions ;
Character category (Character Classes)
- .
- Match line terminator (\n \r \u2028 or \u2029) Any single character other than
- stay
Character set (Character Sets)
in ,. Will lose its special meaning , Represents the original value
- \
- Escape character , It has two meanings
- Indicates that the next character with a special meaning is literal
- Indicates that the next character has a special meaning ( The result of escape is agreed in the metacharacter )
- Indicates that the next character with a special meaning is literal
- Escape character , It has two meanings
- \d Match any Arabic numeral character
- \D Match any non Arabic character
- \w Match any ( Letter 、 Numbers 、 Underline ) The characters of
- \W Match any non ( Letter 、 Numbers 、 Underline ) The characters of
- \s Match a blank , Including Spaces 、 tabs 、 Page identifier 、 Line breaks and other Unicode Space
- \S Match a non blank character
- \t Match a horizontal tab (tab)
- \r Match a carriage return (carriage return)
- \n Match a line break (linefeed)
- \v Match a vertical tab (vertical tab)
- \f Match a page break (form-feed)
^ start $ ending ; \w: Numbers Letter Underline ;
Character set
// []: Character set ;
// let str = "dffdabfds123fdabfdb435afdsa";
// let reg = /a|b/g;
// let reg = /[^ab]/g;
// let res = str.replace(reg,"*");
// console.log(res);
// let reg = /\d+/g;
// let reg = /[0-9]+/g;
// let res = str.replace(reg,"*");
// console.log(res);
// let reg = /[0-20]+/g;
// let res = str.replace(reg,"*");
// console.log(res);
// let str = "this knife is 5cM";
// let reg = /[kKCc]?[Mm]/;
// let res = str.replace(reg," rice ");
// console.log(res);
// . ===> [^\r\n]; \d===>[0-9];
// \w ===>[a-zA-Z_0-9];
[xyz]
- A character set , Also called character group . Match any character in the set . You can use hyphens ’-' Specify a range
[xyz]
Is an antisense or supplementary character set , Also called antisense character group . in other words , It matches any character that is not in parentheses . You can also use hyphens ‘-’ Specify a range of characters
/ Border character :\b : Not \w Are called boundaries ;
// let str = "is this a book?";
// let reg = /\bis\b/g;
// let reg = new RegExp("\\bis\\b","g");
// let arr = str.match(reg);
// console.log(arr);
The border
- ^
- Match input start . If more lines (multiline) The sign is set to true, The character will also match a line break (line break) The beginning after the symbol
- $
- Match input end . If more lines (multiline) The sign is set to true, The character will also match a line break (line break) At the end of the front of the character
- \b
- Match a zero width word boundary (zero-width word boundary)
- \B
- Match a non-zero width word boundary (zero-width word boundary)
### grouping
( children )
- have access to () Grouping expressions , Similar to grouping in Mathematics , Also known as children
- Index grouping
- Name groups
- (?…)
- groups attribute
- Capture match
- With capture (capturing) characteristic , The matching results will be saved to ( Subitem result ) in
- (x)
- Non capture match
- No capture (capturing) characteristic , That is, the matching results will not be saved to ( Subitem result ) in
- (?:x)
- Zero width assertion / Pre check (Assertions)
- Used to specify search in certain content ( But it doesn't include that ) Before or after
- Forward zero width assertion / Pre check
- sure
- (?=pattern)
- no
- (?!pattern)
- sure
- Negative zero width assertion / Pre check ( Be careful :ES2018 newly added )
- sure
- (?<=pattern)
- no
- (?<!patten)
- sure
- The difference between capture and zero width assertion
- Capture : The matching content appears in the result but not in the subitem result
- Zero width assertion : It will not appear in the result
backreferences
- \n
- there n Represents a variable , The value is a number , Point to... In the regular expression n A bracket ( Count from the left ) Substring matching in
// (): grouping ;
// let str = "abfdssafdsababljljabssfd";
// ?---{0,1} + --- {1,} * ---{0,} {2};
// let reg = /s{2}/g;
// let reg = /(ab){2}/g; //abb
// let res = str.replace(reg,"*");
// console.log(res);
// let str = "My name is Lilei";
// // LiLei Replace with Li Lei ;
// let reg = /Li(L|l)ei/g;
// let res = str.replace(reg," Li lei ");
// console.log(res);
// Convert time format :2019-10-19 ----> 19/10/2019;
// backreferences ;
// let mytime = "2019-10-19";
// let reg = /(\d{4})-(\d{1,2})-(\d{1,2})/g;
// let res = mytime.replace(reg,"$3/$2/$1");
// // console.log( RegExp.$1 );
// console.log(res);
// Name groups ;?< Group name > ES2018 New features ;
// let str = "$name=zhangsan&age=20";
// let reg = /\$(?<str>\w+)/;
// let res = str.match(reg);
// console.log(res.groups.str);
// Zero width assertion : positive ( Back ) Negative ( front ) ---》 It must be asserted that 、 A negative assertion ;
// es2018
// Positive affirmation ;
// let str = "iphone3iphone4iphone11iphoneNumber";
// // iphone Switch to “ Apple ”;
// let reg = /iphone(?=\d{1,2})/g;
// let res = str.replace(reg," Apple ");
// console.log(res);
// Positive negative assertion ;
// let str = "iphone3iphone4iphone11iphoneNumber";
// // iphone Switch to “ Apple ”;
// let reg = /iphone(?!\d{1,2})/g;
// let res = str.replace(reg," Apple ");
// console.log(res);
// Negative positive assertion ;
// let str = "10px20px30pxipx";
// // px--> Pixels ;
// let reg = /(?<=\d{2})px/g;
// let res = str.replace(reg," Pixels ");
// console.log(res);
// Negative negative assertion ;
// let str = "10px20px30pxipx";
// // px--> Pixels ;
// let reg = /(?<!\d{2})px/g;
// let res = str.replace(reg," Pixels ");
// console.log(res);
Quantitative vocabulary
- x{n}
- n Is a positive integer . The previous pattern x Come on in succession n Time matching
- x{n,m}
- n and m As a positive integer . The previous pattern x Continuous occurrence of at least n Time , at most m Time matching
- x{n,}
- n Is a positive integer . The previous pattern x Continuous occurrence of at least n Time matching
- x*
- Match the previous pattern x 0 Or many times
- x+
- Match the previous pattern x 1 Or many times . Equivalent to {1,}
- x?
- Match the previous pattern x 0 or 1 Time
- x|y
- matching x or y
Matching mode
g
- global, Global mode : Find all matches , Instead of stopping after the first match
i
- ignore, Ignore case mode : Match is not case sensitive
m
- multiple, Multi line mode : Will start and end characters (^ and $) Think of it as working on multiple lines , Instead of just matching the beginning and end of the entire input string
s
- dotAll / singleline Pattern :. Can match newline
u
unicode,unicode Pattern : matching unicode Character set
console.log(/^.$/.test("\uD842\uDFB7")); console.log(/^.$/u.test("\uD842\uDFB7"));
y
- sticky, Viscous mode : Match regular lastIndex Property specifies the character of the position , And if there is no match, do not try to match from any subsequent indexes
Regular tools http://regexper.com
// Matching mode
// g: The global matching
// i: Ignore case ;
// let str = "afdafdAdfafd";
// let reg = /a/gi;
// let res = str.replace(reg,"*");
// console.log(res);
// m Multi line mode
// let str = `adfsf
// dfdsa`;
// let reg = /^\w/gm;
// let res = str.replace(reg,"*");
// console.log(res);
// s . Can match newline
// let str = `<div>som
// e value..</div>`;
// let reg = /<div>.*<\/div>/gs;
// // “\” escape : Convert special meaning into literal meaning ;
// let res = reg.test(str);
// console.log(res);
// console.log(/^.$/.test("\uD842\uDFB7"));
// console.log(/^.$/u.test("\uD842\uDFB7"));
summary
1) Regular creation
2) Regular matching method
3) Metacharacters
4) Character set
5) The border
6) grouping
7) backreferences
8) Quantitative vocabulary
9) Matching mode
Next class preview
- Nodejs01
边栏推荐
- [at] ABC 258g - Triangle triples reachable - violence
- Go language foundation ----- 16 ----- goroutine, GPM model
- experiment.........
- Redis profile
- register关键字
- Client server model
- Go language foundation ----- 07 ----- method
- Redis配置文件
- Huawei switch basic configuration (telnet/ssh login)
- HDMI2.1与HDMI2.0的区别以及转换PD信号。
猜你喜欢
Go language foundation ----- 11 ----- regular expression
Project experience sharing: realize an IR Fusion optimization pass of Shengsi mindspire layer
在浏览器输入url后执行什么
项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
Go language foundation ----- 15 ----- reflection
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
Go language foundation ----- 06 ----- anonymous fields, fields with the same name
Screenshot tool snipaste
LwIP learning socket (application)
Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
随机推荐
优质博客——
Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
EtherCAT state machine transition (ESM)
哪一刻你才发现青春结束了
技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
RM delete file
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
tp3.2和tp5.0的区别
基于RNA的新型癌症疗法介绍
Pat grade a 1027 colors in Mars
一个实习生的CnosDB之旅
Shengsi mindspire is upgraded again, the ultimate innovation of deep scientific computing
Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select
[step on the pit series] MySQL failed to modify the root password
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
【cocos creator】点击按钮切换界面
Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition
Redis profile
Go language foundation ------ 12 ------ JSON
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation