当前位置:网站首页>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
边栏推荐
- 2020年最新字节跳动Android开发者常见面试题及详细解析
- torch. Where can transfer gradient
- According to the salary statistics of programmers in June 2021, the average salary is 15052 yuan. Are you holding back?
- Imx6ull QEMU bare metal tutorial 1: GPIO, iomux, I2C
- AUTOSAR from introduction to mastery 100 lectures (51) - AUTOSAR network management
- web三大组件之Filter、Listener
- Scons build embedded ARM compiler
- Learning notes of AMBA protocol
- Using open to open a file in JNI returns a - 1 problem
- POM of SSM integration xml
猜你喜欢
Stack protector under armcc / GCC
MySQL 8.0.11 download, install and connect tutorials using visualization tools
[point cloud series] multi view neural human rendering (NHR)
LeetCode_ DFS_ Medium_ 695. Maximum area of the island
100000 college students have become ape powder. What are you waiting for?
CMSIS cm3 source code annotation
9419页最新一线互联网Android面试题解析大全
MySQL5.5安装教程
叮~ 你的奖学金已到账!C认证企业奖学金名单出炉
SPI NAND flash summary
随机推荐
Stack protector under armcc / GCC
[point cloud series] learning representations and generative models for 3D point clouds
Common interview questions and detailed analysis of the latest Android developers in 2020
You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!
Conflict between Mui picker and drop-down refresh
Data warehouse - what is OLAP
十万大学生都已成为猿粉,你还在等什么?
Processbuilder tool class
Machine learning -- PCA and LDA
MySQL 8.0.11 download, install and connect tutorials using visualization tools
【快排】215. 数组中的第K个最大元素
AUTOSAR from introduction to mastery 100 lectures (51) - AUTOSAR network management
叮~ 你的奖学金已到账!C认证企业奖学金名单出炉
Ffmpeg common commands
Machine learning -- model optimization
MySQL5. 5 installation tutorial
Common analog keys of ADB shell: keycode
[tensorflow] sharing mechanism
【行走的笔记】
CSDN高校俱乐部“名师高校行”——湖南师范大学站