当前位置:网站首页>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
边栏推荐
- ES6语法总结--下篇(进阶篇 ES6~ES11)
- Time slice polling scheduling of RT thread threads
- Common properties of location
- E-commerce data analysis -- User Behavior Analysis
- gcc 编译选项
- ES6 grammar summary -- Part 2 (advanced part es6~es11)
- C language, log print file name, function name, line number, date and time
- Contiki源码+原理+功能+编程+移植+驱动+网络(转)
- arduino获取随机数
- MySQL数据库面试题
猜你喜欢
Detailed explanation of 5g working principle (explanation & illustration)
Correspondence between STM32 model and contex M
RT-Thread API参考手册
5G工作原理详解(解释&图解)
Basic use of pytest
Detailed explanation of Union [C language]
Several declarations about pointers [C language]
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
ES6语法总结--上篇(基础篇)
R & D thinking 01 ----- classic of embedded intelligent product development process
随机推荐
锂电池基础知识
C语言,log打印文件名、函数名、行号、日期时间
Arduino get random number
Contiki源码+原理+功能+编程+移植+驱动+网络(转)
GNN的第一个简单案例:Cora分类
. elf . map . list . Hex file
MySQL数据库面试题
Pytoch temperature prediction
Pytoch implements simple linear regression demo
Arduino JSON data information parsing
Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
Important methods of array and string
inline详细讲解【C语言】
Reno7 60W超级闪充充电架构
列表的使用
JS数组常用方法的分类、理解和运用
Unit test - unittest framework
Gallery之图片浏览、组件学习
Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
Imgcat usage experience