当前位置:网站首页>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
边栏推荐
- Imgcat usage experience
- JS数组常用方法的分类、理解和运用
- [esp32 learning-2] esp32 address mapping
- Amba, ahb, APB, Axi Understanding
- Raspberry pie tap switch button to use
- arduino获取数组的长度
- Principle and implementation of MySQL master-slave replication
- Implementation scheme of distributed transaction
- Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
- ESP8266使用arduino连接阿里云物联网
猜你喜欢
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
Basic knowledge of lithium battery
Analysis of charging architecture of glory magic 3pro
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)
Vert. x: A simple TCP client and server demo
Understanding of AMBA, AHB, APB and Axi
Reno7 60W super flash charging architecture
AMBA、AHB、APB、AXI的理解
Kaggle competition two Sigma connect: rental listing inquiries
Vscode basic configuration
随机推荐
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
STM32 how to locate the code segment that causes hard fault
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
JS数组常用方法的分类、理解和运用
IOT system framework learning
Raspberry pie tap switch button to use
Distribute wxWidgets application
Embedded startup process
RT-Thread的main线程“卡死”的一种可能原因及解决方案
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
树莓派 轻触开关 按键使用
Kconfig Kbuild
FreeRTOS 任务函数里面的死循环
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
Principle and implementation of MySQL master-slave replication
锂电池基础知识
ESP8266使用arduino连接阿里云物联网
ES6 grammar summary -- Part I (basic)
Basic knowledge of lithium battery