为什么RegExp.prototype.exec()的返回值会变化
来源:互联网 发布:超级优化基因液txt免费 编辑:程序博客网 时间:2024/05/20 00:36
1 环境
node:v6.11.1
2 问题
为了方便,我在node环境下测试RegExp.exec()
测试结果如下:
我们可以看到:同样的代码,返回的结果是交替变化的这是为什么呢?
3 原因
还是要看文档:
Finding successive matches
If your regular expression uses the “g” flag, you can use the exec() method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression’s lastIndex property .
文档里说正则表达式有一个lastIndex属性,每次搜索都会从这个属性指明的位置开始。而这个属性每次匹配成功后都会更新到匹配结束的位置。
我们从下图红框中的内容可以看到:lastIndex是被更新了。
这就是为什么exec
返回的结果交替变化的原因:每次搜索的范围变了。
阅读全文
0 0
- 为什么RegExp.prototype.exec()的返回值会变化
- 保存exec的返回值
- request.getCharacterEncoding() 的返回值为什么会是null
- RegExp里的match与exec
- 关于 RegExp exec()方法的学习
- RegExp对象的exec方法应用
- mysql_insert_id 为什么会返回空值
- 有关EXEC返回值的问题.
- javascript RegExp exec()
- 读取exec返回值
- 读取exec返回值
- js正则exec()和test()的区别,RegExp.$1意义
- regExp的test方法取得的值变化的原因及处理办法
- 为什么findViewById会返回null
- Linux命令返回值为什么有时会和预计的不一样
- fork & exec时打开文件的变化
- 在同一个地点为什么GPS的经纬度还是会不断的变化?
- 在同一个地点为什么GPS的经纬度还是会不断的变化?
- ubuntu16.04安装qt5
- Leetcode655. Print Binary Tree
- codeforces 339D 简单的线段树操作
- 【HDU1950】Bridging signals (最长上升子序列DP(nlogn))
- AFNetworking 请求工具类
- 为什么RegExp.prototype.exec()的返回值会变化
- 【矩阵快速幂】HDU_2157_How many ways??
- 高速缓存区dns
- [BZOJ1004][HNOI2008]Cards-Burnside引理
- Matlab中的静态变量和全局变量
- Spring AOP原理分析一次看懂
- react-navigation
- SSM(五)基于webSocket的聊天室
- struts2--基于xml文件的验证器