javascript 中String.match()与RegExp.exec()的区别说明


Posted in Javascript onJanuary 10, 2013

1. 这两个方法,如果匹配成功,返回一个数组,匹配失败,返回null。
2. 当RegExp的global属性为false时,这两个方法的返回数组是一样的。

数组的第0个元素是整个pattern的第一个匹配字符串,接下来的元素是pattern第一个匹配中的子匹配字符串。

此外,数组还有index和input两个额外属性,index是匹配字符串的起始位置,input是整个输入字符串。

此时,RegExp的lastIndex属性一直是0。
demo:

var s = 'this is a string'; 
var p = /\b\w*(i)s\b/; 
var rm = s.match(p); 
var re = p.exec(s); 
console.log('match_array: ' + JSON.stringify(rm)); 
console.log('match_array_index: ' + rm.index); 
console.log('match_array_input: ' + rm.input); 
console.log('----------------------------'); 
console.log('exec_array: ' + JSON.stringify(re)); 
console.log('exec_array_index: ' + re.index); 
console.log('exec_array_input: ' + re.input);

显示结果为(firefox控制台):
match_array: ["this","i"] 
match_array_index: 0 
match_array_input: this is a string 
---------------------------- 
exec_array: ["this","i"] 
exec_array_index: 0 
exec_array_input: this is a string

3. 当RegExp的global属性为true时,返回的数组是不同的。

match方法返回的数组包含着所有匹配字符串,没有子匹配字符串和额外属性。此时,lastIndex属性无效。

exec方法返回的数组格式与global为false时一样,只是此时RegExp的lastIndex属性有效,匹配是从lastIndex所指示的字符开始的,并且方法执行后会将lastIndex置为本次匹配字符串的下一个字符处,所以循环执行exec方法时会依次匹配整个字符串,直到字符串最后返回null,并将lastIndex置0。
demo:

var s = 'this is a string'; 
var p = /\b\w*(i)s\b/g; 
var rm = s.match(p); 
var re; 
console.log('match_array: ' + JSON.stringify(rm)); 
console.log('match_array_index: ' + rm.index); 
console.log('match_array_input: ' + rm.input); 
while(re = p.exec(s)){ 
console.log('----------------------------'); 
console.log('exec_array: ' + JSON.stringify(re)); 
console.log('exec_array_index: ' + re.index); 
console.log('exec_array_input: ' + re.input); 
console.log('regexp_lastIndex: ' + p.lastIndex); 
} 
console.log('----------------------------'); 
console.log('exec_array: ' + re); 
console.log('regexp_lastIndex: ' + p.lastIndex);

结果:
match_array: ["this","is"] 
match_array_index: undefined 
match_array_input: undefined 
---------------------------- 
exec_array: ["this","i"] 
exec_array_index: 0 
exec_array_input: this is a string 
regexp_lastIndex: 4 
---------------------------- 
exec_array: ["is","i"] 
exec_array_index: 5 
exec_array_input: this is a string 
regexp_lastIndex: 7 
---------------------------- 
exec_array: null 
regexp_lastIndex: 0

综上:

1.在没有g标识符时,match和exec方法效果是一样的;有g标识符时,exec方法可以提供最完整的匹配结果。
2.这里顺便提一下RegExp.test()方法,它是exec方法的简化版,有匹配结果就返回true,没有匹配结果就返回false,执行过程与exec是一样的。相当于 (p.exec(s) != null)。
3.RegExp的lastIndex属性在有g标识符,且在exec和test方法中是有效的,其他地方是无效的。

Javascript 相关文章推荐
javascript操作数组详解
Dec 17 Javascript
JS实现往下不断流动网页背景的方法
Feb 27 Javascript
javascript鼠标右键菜单自定义效果
Dec 08 Javascript
input 标签实现输入框带提示文字效果(两种方法)
Oct 09 Javascript
Vue.js在数组中插入重复数据的实现代码
Nov 17 Javascript
jQuery实现获取选中复选框的值实例详解
Jun 28 jQuery
详解swipe使用及竖屏页面滚动方法
Jun 28 Javascript
vue.js项目 el-input 组件 监听回车键实现搜索功能示例
Aug 25 Javascript
three.js实现炫酷的全景3D重力感应
Dec 30 Javascript
Vue的编码技巧与规范使用详解
Aug 28 Javascript
深入了解JavaScript词法作用域
Jul 29 Javascript
Vue中正确使用Element-UI组件的方法实例
Oct 13 Javascript
防止文件缓存的js代码
Jan 10 #Javascript
js修改table中Td的值(定义td的单击事件)
Jan 10 #Javascript
js修改table中Td的值(定义td的双击事件)
Jan 10 #Javascript
javascript之Partial Application学习
Jan 10 #Javascript
javascript之典型高阶函数应用介绍二
Jan 10 #Javascript
javascript之典型高阶函数应用介绍
Jan 10 #Javascript
根据json字符串生成Html的一种方式
Jan 09 #Javascript
You might like
使用网络地址转换实现多服务器负载均衡
2006/10/09 PHP
如何设置mysql允许外网访问
2013/06/04 PHP
php while循环得到循环次数
2013/10/26 PHP
关于Laravel Route重定向的一个注意点
2017/01/16 PHP
PHP如何获取当前主机、域名、网址、路径、端口等参数
2017/06/09 PHP
EasyUI中的tree用法介绍
2011/11/01 Javascript
jQuery.query.js 取参数的两点问题分析
2012/08/06 Javascript
js实现两个值相加alert出来精确到指定位
2013/09/25 Javascript
JS实现控制表格内指定单元格内容对齐的方法
2015/03/30 Javascript
JS区分Object与Aarry的六种方法总结
2017/02/27 Javascript
JS对象深度克隆实例分析
2017/03/16 Javascript
详解vue的diff算法原理
2018/05/20 Javascript
最简单的JS实现json转csv的方法
2019/01/10 Javascript
vue项目移动端实现ip输入框问题
2019/03/19 Javascript
简单了解vue 插值表达式Mustache
2020/07/22 Javascript
nginx配置域名后的二级目录访问不同项目的配置操作
2020/11/06 Javascript
Vite和Vue CLI的优劣
2021/01/30 Vue.js
python批量修改文件名的实现代码
2014/09/01 Python
Python实现抓取城市的PM2.5浓度和排名
2015/03/19 Python
Python2.x与Python3.x的区别
2016/01/14 Python
Python 转义字符详细介绍
2017/03/21 Python
Python Django Vue 项目创建过程详解
2019/07/29 Python
Python各种扩展名区别点整理
2020/02/27 Python
Python3 利用face_recognition实现人脸识别的方法
2020/03/13 Python
Spark处理数据排序问题如何避免OOM
2020/05/21 Python
如何打包Python Web项目实现免安装一键启动的方法
2020/05/21 Python
Appium+Python实现简单的自动化登录测试的实现
2021/01/26 Python
HTML5 绘制图像(上)之:关于canvas元素引领下一代web页面的问题
2013/04/24 HTML / CSS
Fnac西班牙官网:法国文化和电子产品零售商
2021/03/14 全球购物
婚庆公司的创业计划书
2014/01/22 职场文书
甜点店创业计划书
2014/01/27 职场文书
教师师德师风个人整改方案
2014/09/18 职场文书
2014年监理工作总结范文
2014/11/17 职场文书
办公室卫生管理制度
2015/08/04 职场文书
婚宴祝酒词大全
2015/08/10 职场文书
团队拓展训练心得体会
2016/01/12 职场文书