当前位置:网站首页>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
边栏推荐
猜你喜欢
LeetCode_ DFS_ Medium_ 695. Maximum area of the island
MySQL 8.0.11下载、安装和使用可视化工具连接教程
The first lesson is canvas, showing a small case
2021年6月程序员工资统计,平均15052元,你拖后腿了吗?
初鉴canvas,展示个小小的小案例
解决Oracle中文乱码的问题
Android clear app cache
Solve the problem that Oracle needs to set IP every time in the virtual machine
nodejs + mysql 实现简单注册功能(小demo)
Summary of request and response and their ServletContext
随机推荐
mysql 基本语句查询
Conflict between Mui picker and drop-down refresh
Stack protector under armcc / GCC
【快排】215. 数组中的第K个最大元素
Solve the problem of Oracle Chinese garbled code
Data warehouse - what is OLAP
这几种 VSCode 扩展是我最喜欢的
[multi screen interaction] realize dual multi screen display II: startactivity mode
榜样专访 | 孙光浩:高校俱乐部伴我成长并创业
普通大学生如何拿到大厂offer?敖丙教你一招致胜!
[point cloud series] summary of papers related to implicit expression of point cloud
Lpddr4 notes
TERSUS笔记员工信息516-Mysql查询(2个字段的时间段唯一性判断)
Armv8m (cortex M33) MPU actual combat
Processbuilder tool class
【行走的笔记】
100 GIS practical application cases (34) - splicing 2020globeland30
SPI NAND flash summary
CSDN高校俱乐部“名师高校行”——湖南师范大学站
AUTOSAR from introduction to mastery 100 lectures (50) - AUTOSAR memory management series - ECU abstraction layer and MCAL layer