当前位置:网站首页>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
边栏推荐
- 锂电池基础知识
- Vert. x: A simple TCP client and server demo
- Embedded startup process
- 機器學習--線性回歸(sklearn)
- 互联网协议详解
- Working principle of genius telephone watch Z3
- Understanding of AMBA, AHB, APB and Axi
- RT-Thread API参考手册
- E-commerce data analysis -- salary prediction (linear regression)
- MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
猜你喜欢
open-mmlab labelImg mmdetection
[esp32 learning-2] esp32 address mapping
Several declarations about pointers [C language]
Understanding of AMBA, AHB, APB and Axi
C language callback function [C language]
Basic knowledge of lithium battery
Variable star user module
Implementation scheme of distributed transaction
STM32 如何定位导致发生 hard fault 的代码段
几个关于指针的声明【C语言】
随机推荐
Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
ESP学习问题记录
Dependency in dependencymanagement cannot be downloaded and red is reported
JS object and event learning notes
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
ToggleButton实现一个开关灯的效果
ES6语法总结--上篇(基础篇)
RT thread API reference manual
Detailed explanation of Union [C language]
Embedded startup process
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
Use of lists
AMBA、AHB、APB、AXI的理解
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
ESP8266通过arduino IED连接巴法云(TCP创客云)
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
Common properties of location
C language callback function [C language]
C语言函数之可变参数原理:va_start、va_arg及va_end
选择法排序与冒泡法排序【C语言】