当前位置:网站首页>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
边栏推荐
- mui 关闭其他页面,只保留首页面
- UEFI learning 01-arm aarch64 compilation, armplatformpripeicore (SEC)
- Bottomsheetdialogfragment + viewpager + fragment + recyclerview sliding problem
- [point cloud series] foldingnet: point cloud auto encoder via deep grid deformation
- Brief introduction of asynchronous encapsulation interface request based on uniapp
- Machine learning -- model optimization
- How do ordinary college students get offers from big factories? Ao Bing teaches you one move to win!
- Office 2021 installation package download and activation tutorial
- Stack protector under armcc / GCC
- 在 pytorch 中加载和使用图像分类数据集 Fashion-MNIST
猜你喜欢

AUTOSAR from introduction to mastery 100 lectures (52) - diagnosis and communication management function unit

2020最新Android大厂高频面试题解析大全(BAT TMD JD 小米)

Request和Response及其ServletContext总结

100000 college students have become ape powder. What are you waiting for?

CMSIS cm3 source code annotation

面试官给我挖坑:单台服务器并发TCP连接数到底可以有多少 ?

kettle庖丁解牛第16篇之输入组件周边讲解

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?

mui + hbuilder + h5api模拟弹出支付样式

交叉碳市场和 Web3 以实现再生变革
随机推荐
[point cloud series] learning representations and generative models for 3D point clouds
CSDN高校俱乐部“名师高校行”——湖南师范大学站
Processbuilder tool class
Lpddr4 notes
X509 parsing
9419 page analysis of the latest first-line Internet Android interview questions
Loading and using image classification dataset fashion MNIST in pytorch
MySQL 8.0.11下载、安装和使用可视化工具连接教程
[point cloud series] deepmapping: unsupervised map estimation from multiple point clouds
基于uniapp异步封装接口请求简介
普通大学生如何拿到大厂offer?敖丙教你一招致胜!
POM of SSM integration xml
Esp32 vhci architecture sets scan mode for traditional Bluetooth, so that the device can be searched
hbuilderx + uniapp 打包ipa提交App store踩坑记
vscode小技巧
MySQL basic statement query
浅谈js正则之test方法bug篇
Mui + hbuilder + h5api simulate pop-up payment style
UEFI learning 01-arm aarch64 compilation, armplatformpripeicore (SEC)
Nodejs + websocket cycle small case