当前位置:网站首页>JS regular expression basic knowledge learning
JS regular expression basic knowledge learning
2022-07-06 12:09:00 【Aboci Bang】
Create regular objects
1. Literal var t = /1/;
2. Instantiate objects var t = new RegExp(‘1’); // Slash no Use quotation marks
Matching mode ( Modifier ) Add... After the slash
g – The global matching
i – Ignore case
var t = /a/i; // Match uppercase A and A lowercase letter a
var t = new RegExp(‘a’,‘i’);
Common methods
1. Regular objects provide regular methods
(1) exec(str) // Returns the string matching part No match returned null
(2) test(str) // Matches the specified string return true false
2.String Object
(1)var z = ‘sssscaa555’.search(/a/); // Returns the position subscript of the first occurrence of the matched string
(2)z = ‘sssscaa555’.match(/a/g); // Return the first matching result in the form of an array If added g Return all results as an array
(3)z = ‘sssscaa555’.replace(/a/); // Replace the specified string If added g Replace all matching strings
(4)z = ‘sssscaa555’.split(‘s’); // The matched string is node Division Specified string Global mode is used by default 

// Match the first a And replace with *
/g and replaceAll() : Match all a And replace with *

Commonly used search characters ( according to ASCII Code table to find )
[a-z] // matching a To z Characters between
[A-Z]
[0-9]
[a-c0-5A-G]
[adghj]
[^abc] // except abc Any character outside ( In square brackets ^ Representation inversion )
\d // Match numeric characters
\D // Match non numeric characters
\w // Match alphanumeric underscores
\W // Match non alphanumeric underscores Equivalent to [^\w]
\s // Matching blank character
\S // Match non white space characters Equivalent to [^\s]
. // Match any character except carriage return (\n)
[\u4e00-\u9fa5] // Match any Chinese characters \u On behalf of the use of 16 Base number 
subexpression
The expression in parentheses of regular expression is a subexpression /a(d)/ Used to provide values in the cache for subsequent matches
Capture
The subexpression matching content is put into the cache This process is called capture , Every time a matching character is obtained Just put it into a new cache We can label the cache ($1 $2 $3), The reason why there is capture Because of the data in the cache Subsequent regular matching can call cached data More convenient matching .
backreferences
After the subexpression The remaining regularities of use the characters matched by the front sub expression This process is called reverse reference .
‘dasdsadsaaaa’.match(/(a)\1\1\1/); // return aaaa
‘12221221’.match(/(\d)(\d)\2\1/); // return 1221
qualifiers
(*) asterisk Match the front face expression 0 Times or times
(+) plus Match the front face expression 1 Times or times
? Match the front face expression 0 Time or 1 Time
{n} matching n Time
{n,} At least n Time
{n,m} Match at least n~m Time Between It's OK to exceed 

Locator
^ Put it outside square brackets =》 The starting position of the matching string The first character of a string
$ The end of the matching string The last character of the string
\b Matches a word boundary ‘an’.match(/\ban\b/); Means to find a word :an
\B Match non word boundaries 
Special character escape
The backslash Regular expressions are also used , Therefore, if the string matches, it should be escaped
~~ This is just an example Please Baidu to match the regular website
// Here we escape the backslash \/\/
/^[a-zA-Z]{
1,4}:\/\/*.[a-zA-Z]{1,6}/.test('http://www.baidu.com');

perhaps ( | ) Usage of
‘sb’.match(/\b(sb|dsb)\b/g); // Match custom string 
Greedy mode ( Match multiple numbers )
Regular default matches more Less mismatches Hardworking little bee {} The number of matching numbers in the middle Default match 9 individual 
Non greedy model ( Fewer matches )
Inertia matching Give priority to those with less matching Add stay {} Back plus ? {} The number of matching numbers in the middle matching 3 individual 
Pre check
The specified string has not been matched But first preview the following string
1、 Forward lookahead ( Assertion ): Check from left to right
// Inquire about With 136 For the beginning of the cell-phone number
' 13658542165 15236578954 13654741254'.match(/\b(?=136)\d+\b/g);
// Inquire about Don't to 136 For the beginning of the cell-phone number hold = Change to !
' 13658542165 15236578954 13654741254'.match(/\b(?!136)\d+\b/g);
2、 Reverse Preview : Check from the back right to the left
// matching hello word Look from right to left It's a match olleh After matching Look again h hinder 3 Then stop
"123hello123".match(/(?<=3)hello/g);

~~ Video learning recommendations b standing 【 Black horse programmer 】
https://www.bilibili.com/video/BV1uC4y187dF?spm_id_from=333.337.search-card.all.click
边栏推荐
- Unit test - unittest framework
- Basic use of pytest
- Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
- ESP8266使用arduino连接阿里云物联网
- 几个关于指针的声明【C语言】
- OPPO VOOC快充电路和协议
- Dependency in dependencymanagement cannot be downloaded and red is reported
- . elf . map . list . Hex file
- FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
- A possible cause and solution of "stuck" main thread of RT thread
猜你喜欢
![Several declarations about pointers [C language]](/img/9b/ace0abbd1956123a945a98680b1e86.png)
Several declarations about pointers [C language]

ES6语法总结--上篇(基础篇)

STM32 how to locate the code segment that causes hard fault

Kaggle competition two Sigma connect: rental listing inquiries (xgboost)

Togglebutton realizes the effect of switching lights

基于Redis的分布式ID生成器

荣耀Magic 3Pro 充电架构分析

Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)

Working principle of genius telephone watch Z3

高通&MTK&麒麟 手機平臺USB3.0方案對比
随机推荐
List and set
ToggleButton实现一个开关灯的效果
C language, log print file name, function name, line number, date and time
C语言函数之可变参数原理:va_start、va_arg及va_end
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
Pytorch实现简单线性回归Demo
JS数组常用方法的分类、理解和运用
Kaggle competition two Sigma connect: rental listing inquiries
机器学习--决策树(sklearn)
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
GNN的第一个简单案例:Cora分类
R & D thinking 01 ----- classic of embedded intelligent product development process
Common properties of location
FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
[template] KMP string matching
RT-Thread API参考手册
I2C总线时序详解
Implementation scheme of distributed transaction
Basic use of pytest