当前位置:网站首页>On the bug of JS regular test method
On the bug of JS regular test method
2022-04-23 13:24:00 【mb625d3d7cd8cc1】
Actually, I seldom use this , So I didn't pay attention to this problem before , I went to see this thing only after defoliation wrote a abnormal test
Actually, I seldom use this , So I didn't pay attention to this problem before , I went to see this thing only after defoliation wrote a abnormal test
The following code is all in chrome Of F12 Next debugging , You can study
Let's start with something .
var re = /\d/;
console.log( re.test("1") );
console.log( re.test("1") );
console.log( re.test("1") );
console.log( re.test("1") );
- 1.
- 2.
- 3.
- 4.
- 5.
All is true That's all right. ..
But you put /\d/; Change to /\d/g; Try again. .
Revise again :
console.log( /\d/g.test("1") );
console.log( /\d/g.test("1") );
console.log( /\d/g.test("1") );
console.log( /\d/g.test("1") );
- 1.
- 2.
- 3.
- 4.
All is true, Why on earth is this ?
These results are quite interesting , Of course, experts naturally know why , If you know , In fact, you can skip the following without looking , It's all hydrology ..
There is one in the regular lastIndex
Properties of , It's the start of the next match .
var re = /\d/g;
console.log( re.test("1"), re.lastIndex );
console.log( re.test("1"), re.lastIndex );
console.log( re.test("1"), re.lastIndex );
console.log( re.test("1"), re.lastIndex );
- 1.
- 2.
- 3.
- 4.
- 5.
You can see The first matching result is true
Indicates that the match is successful , here lastIndex
Record the starting position of the next match as 1.
So the second match from "1" String index 1 Location matching for , Of course, the match failed , Because this string has only one character , His index is 0.
and /\d/g.test("1")
This is why every successful match can ?
Because it directly uses regular literals , It's equivalent to recreating one regular object at a time ,lastIndex
The initial value of the property is 0.
So you can match successfully every time .
Now do you understand , Include exec
It's the same thing , Match one at a time ,lastIndex
Record the starting position of the next match .
If you have to use a regular object , Then only every time test
Pre reset lastIndex
了 , So we can make sure he doesn't have an accident .
var re = /\d/g;
console.log( re.test("1") );
re.lastIndex = 0;
console.log( re.test("1") );
re.lastIndex = 0;
console.log( re.test("1") );
re.lastIndex = 0;
console.log( re.test("1") );
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
Okay , It took a lot of time to modify the syntax highlighting plug-in today , So water wrote an article , I hope you will have a better understanding .
版权声明
本文为[mb625d3d7cd8cc1]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231151524455.html
边栏推荐
- Using open to open a file in JNI returns a - 1 problem
- 100 GIS practical application cases (51) - a method for calculating the hourly spatial average of NC files according to the specified range in ArcGIS
- LeetCode_DFS_中等_695.岛屿的最大面积
- nodejs + mysql 实现简单注册功能(小demo)
- SSM整合之pom.xml
- UEFI learning 01-arm aarch64 compilation, armplatformpripeicore (SEC)
- Scons build embedded ARM compiler
- Analysis of the latest Android high frequency interview questions in 2020 (BAT TMD JD Xiaomi)
- vscode小技巧
- 【行走的笔记】
猜你喜欢
榜样专访 | 孙光浩:高校俱乐部伴我成长并创业
./gradlew: Permission denied
[point cloud series] relationship based point cloud completion
你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!
【官宣】长沙软件人才实训基地成立!
[point cloud series] full revolutionary geometric features
nodejs + mysql 实现简单注册功能(小demo)
[point cloud series] learning representations and generative models for 3D point clouds
[point cloud series] Introduction to scene recognition
Nodejs + Mysql realize simple registration function (small demo)
随机推荐
"Xiangjian" Technology Salon | programmer & CSDN's advanced road
[indicators] precision, recall
LeetCode_ DFS_ Medium_ 695. Maximum area of the island
100 GIS practical application cases (52) - how to keep the number of rows and columns consistent and aligned when cutting grids with grids in ArcGIS?
SHA512 / 384 principle and C language implementation (with source code)
【动态规划】221. 最大正方形
TERSUS笔记员工信息516-Mysql查询(2个字段的时间段唯一性判断)
Launcher hides app icons that do not need to be displayed
Short name of common UI control
These vscode extensions are my favorite
C语言之字符串与字符数组的区别
9419 page analysis of the latest first-line Internet Android interview questions
Nodejs + websocket cycle small case
Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
Loading and using image classification dataset fashion MNIST in pytorch
AUTOSAR from introduction to mastery 100 lectures (83) - bootloader self refresh
hbuilderx + uniapp 打包ipa提交App store踩坑记
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation
你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!