JS正则中test方法
来源:互联网 发布:张家界导游软件app 编辑:程序博客网 时间:2024/05/16 15:46
这个方法经常用于测试是否满足条件, 但是有这种情况可能不是我们想要的:
var re = /a/g;console.log(re.test('a'));console.log(re.test('a'));
感觉很奇怪, 上面代码的结果是true, false, 这不是我们希望的, 按道理讲, 应该都是true才对.
出现这种现象的原因是因为lastIndex, 测试如下
var re = /a/g;console.log(re.test('a'), re.lastIndex);console.log(re.test('a'), re.lastIndex);
lastIndex值分别是1, 0, 也就是说执行第二个test后, lastIndex的值(2)由于已经超出了输入串(‘a’)的长度, 会被重置为0
这样的话, 我们以后就不要在调用test方法时添加g标志就好了.
0 0
- JS正则中test方法
- JS正则test()方法
- JS正则test()方法
- JS正则表达式-test()方法的使用
- js正则表达式test方法的问题
- js正则表达式中test,exec,match方法的区别说明
- js正则表达式中test,exec,match方法的区别说明
- JS中正则匹配的3个方法:match,exec,test
- javascript正则表达式(5)——JS中test()与exec()方法
- js正则表达式中test,exec,match方法的区别说明
- js正则表达式中test,exec,match方法的区别说明
- js 正则test问题
- js 中test,exec,match方法
- Js 正则表达式对象(方法:exec、test、match、search)
- js正则方法test与match简单介绍
- JS 中 正则表达式 match test exec的区别
- js正则函数中test和match的区别
- js用正则表达式来验证表单(比较齐全的资源),JS正则test()方法
- 无名管道的使用
- HDU1789 Doing Homework again 贪心
- codeforces 715B. Complete The Graph
- Oracle 数据库导出全库(包含空表)
- 螺旋矩阵
- JS正则中test方法
- 解决xcode 8 连接真机时build项目时提示xxx is automatically signed, but code signing identity iPhone Developer的问题
- Google APAC Test 2017 Round D
- 树的高度和深度的区别
- FAT和NTFS文件系统
- Linux进程间的通信--管道通信
- 设置redis可以远程访问
- 学习博客
- linux 使用共享内存实现生产者消费者,PV操作。