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 相关文章推荐
实现png图片和png背景透明(支持多浏览器)的方法
Sep 08 Javascript
jQuery实现 注册时选择阅读条款 左右移动
Apr 11 Javascript
javascript数组输出的两种方式
Jan 13 Javascript
js实现简单排列组合的方法
Jan 27 Javascript
canvas绘制一个常用的emoji表情
Mar 30 Javascript
angular2路由之routerLinkActive指令【推荐】
May 30 Javascript
layer.confirm取消按钮绑定事件的方法
Aug 17 Javascript
vue车牌号校验和银行校验实战
Jan 23 Javascript
详解微信图片防盗链“此图片来自微信公众平台 未经允许不得引用”的解决方案
Apr 04 Javascript
详解async/await 异步应用的常用场景
May 13 Javascript
Node.js系列之连接DB的方法(3)
Aug 30 Javascript
vue实现登录拦截
Jun 29 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
PHP实现简单汉字验证码
2015/07/28 PHP
php array_values 返回数组的值实例详解
2016/11/17 PHP
thinkPHP中_initialize方法实例分析
2016/12/05 PHP
thinkphp3.2实现在线留言提交验证码功能
2017/07/19 PHP
PHP实现函数内修改外部变量值的方法示例
2018/12/28 PHP
学习YUI.Ext第五日--做拖放Darg&Drop
2007/03/10 Javascript
url 特殊字符 传递参数解决方法
2010/01/01 Javascript
用apply让javascript函数仅执行一次的代码
2010/06/27 Javascript
通过继承IHttpHandle实现JS插件的组织与管理
2010/07/13 Javascript
简略的前端架构心得&&基于editor为例子的编码小技巧
2010/11/25 Javascript
另一个javascript小测验(代码集合)
2011/07/27 Javascript
javascript修改表格背景色实例代码分享
2013/12/10 Javascript
JS实现鼠标单击与双击事件共存
2014/03/08 Javascript
关于JavaScript命名空间的一些心得
2014/06/07 Javascript
一句jQuery代码实现返回顶部效果(简单实用)
2016/12/28 Javascript
AngularJS页面传参的5种方式
2017/04/01 Javascript
jQuery滑动到底部加载下一页数据的实例代码
2017/05/22 jQuery
layui中layer前端组件实现图片显示功能的方法分析
2017/10/13 Javascript
JS数组去重的6种方法完整实例
2018/12/08 Javascript
JS实现横向跑马灯效果代码
2020/04/20 Javascript
Node Mongoose用法详解【Mongoose使用、Schema、对象、model文档等】
2020/05/13 Javascript
JavaScript实现下拉列表
2021/01/20 Javascript
Python中编写ORM框架的入门指引
2015/04/29 Python
Python实现基于KNN算法的笔迹识别功能详解
2018/07/09 Python
如何通过python画loss曲线的方法
2019/06/26 Python
python设置代理和添加镜像源的方法
2020/02/14 Python
体育教育毕业生自荐信
2013/11/21 职场文书
应届大学生求职信
2013/12/01 职场文书
20年同学聚会邀请函
2014/02/04 职场文书
森林防火工作方案
2014/02/14 职场文书
初中国旗下的演讲稿
2014/08/28 职场文书
世界卫生日宣传活动总结
2015/02/09 职场文书
员工辞退通知书
2015/04/17 职场文书
2015年维修工作总结
2015/04/25 职场文书
评测 | 大屏显示带收音机的高端音箱,JBL TUNE2便携式插卡音箱实测
2021/04/24 无线电
MySql如何将查询的出来的字段进行转换
2022/06/14 MySQL