当前位置:网站首页>浅谈js正则之test方法bug篇
浅谈js正则之test方法bug篇
2022-04-23 13:19:00 【mb625d3d7cd8cc1】
其实我很少用这个,所以之前一直没注意这个问题,自从落叶那厮写了个变态的测试我才去看了下这东西
其实我很少用这个,所以之前一直没注意这个问题,自从落叶那厮写了个变态的测试我才去看了下这东西
下面的代码都是在chrome的F12下调试的,大家可以研究一下
先来看个东西吧。
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.
全部是 true 没问题。。
但是你把 /\d/; 改成 /\d/g; 再试试。
再次修改:
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.
全部是 true,这究竟是为什么呢?
这些结果相当有意思,当然高手自然知道为什么,如果你知道的话,下面其实可以跳过不用看了,全是水文而已。。
正则里有一个 lastIndex
的属性,是下一次匹配的开始位置。
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.
可以看到 第一次匹配结果为 true
表示匹配成功,此时 lastIndex
记录下一次匹配的起始位置为 1。
于是第二次匹配的时候 从 "1" 字符串索引 1 的位置匹配,当然就匹配失败了,因为这个字符串只有一个字符,他的索引是 0。
而 /\d/g.test("1")
这个为什么每次匹配成功能呢?
因为它直接用正则字面量,相当于每次重新创建一个正则对象,lastIndex
属性的初始值是 0。
所以每次都能匹配成功。
现在是不是理解了,包括 exec
也一样,每次匹配一个,lastIndex
记录下次匹配的起始位置。
如果非要用一个正则对象的,那就只有每次 test
前重置 lastIndex
了,这样才能保证他不出意外。
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.
好了,今天修改语法高亮插件花了不少时间,所以水了一篇,望大家海涵。
版权声明
本文为[mb625d3d7cd8cc1]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_15605879/5248422
边栏推荐
- STM32 tracking based on open MV
- 51 single chip microcomputer stepping motor control system based on LabVIEW upper computer (upper computer code + lower computer source code + ad schematic + 51 complete development environment)
- [dynamic programming] 221 Largest Square
- AUTOSAR from introduction to mastery 100 lectures (83) - bootloader self refresh
- The first lesson is canvas, showing a small case
- XML
- Kernel error: no rule to make target 'Debian / canonical certs pem‘, needed by ‘certs/x509_ certificate_ list‘
- Super 40W bonus pool waiting for you to fight! The second "Changsha bank Cup" Tencent yunqi innovation competition is hot!
- 超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
- FFmpeg常用命令
猜你喜欢
100 lectures on practical application cases of Excel (VIII) - report connection function of Excel
Ding ~ your scholarship has arrived! C certified enterprise scholarship list released
vscode小技巧
【快排】215. 数组中的第K个最大元素
AUTOSAR from introduction to mastery 100 lectures (81) - FIM of AUTOSAR Foundation
Request和Response及其ServletContext总结
web三大组件之Servlet
SHA512 / 384 principle and C language implementation (with source code)
How do ordinary college students get offers from big factories? Ao Bing teaches you one move to win!
【动态规划】221. 最大正方形
随机推荐
torch. Where can transfer gradient
CSDN高校俱乐部“名师高校行”——湖南师范大学站
Conflict between Mui picker and drop-down refresh
According to the salary statistics of programmers in June 2021, the average salary is 15052 yuan. Are you holding back?
You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!
AUTOSAR from introduction to mastery 100 lectures (52) - diagnosis and communication management function unit
Introduction to metalama 4 Use fabric to manipulate items or namespaces
[indicators] precision, recall
[Journal Conference Series] IEEE series template download guide
Imx6ull QEMU bare metal tutorial 2: usdhc SD card
nodejs + mysql 实现简单注册功能(小demo)
9419页最新一线互联网Android面试题解析大全
[point cloud series] deepmapping: unsupervised map estimation from multiple point clouds
Esp32 vhci architecture sets scan mode for traditional Bluetooth, so that the device can be searched
Uninstall MySQL database
decast id.var measure. Var data splitting and merging
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
基于uniapp异步封装接口请求简介
UEFI learning 01-arm aarch64 compilation, armplatformpripeicore (SEC)
[51 single chip microcomputer traffic light simulation]