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 相关文章推荐
解决表单中第一个非隐藏的元素获得焦点的一个方案
Oct 26 Javascript
JS判断是否长按某一键的方法
Mar 02 Javascript
Kindeditor在线文本编辑器如何过滤HTML
Apr 14 Javascript
最全正则表达式总结:验证QQ号、手机号、Email、中文、邮编、身份证、IP地址等
Aug 16 Javascript
JS设计模式之单例模式(一)
Sep 29 Javascript
bmob js-sdk 在vue中的使用教程
Jan 21 Javascript
Vue实现点击后文字变色切换方法
Feb 11 Javascript
JS实现的JSON数组去重算法示例
Apr 11 Javascript
jsonp跨域获取数据的基础教程
Jul 01 Javascript
vee-validate vue 2.0自定义表单验证的实例
Aug 28 Javascript
对Vue2 自定义全局指令Vue.directive和指令的生命周期介绍
Aug 30 Javascript
原生JS实现音乐播放器的示例代码
Feb 25 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警告Creating default object from empty value 问题的解决方法
2014/04/02 PHP
YII2框架中日志的配置与使用方法实例分析
2020/03/18 PHP
游戏人文件夹程序 ver 4.03
2006/07/14 Javascript
Javascript 复制数组实现代码
2009/11/26 Javascript
chrome浏览器不支持onmouseleave事件的解决技巧
2013/05/31 Javascript
用jquery生成二级菜单的实例代码
2013/06/24 Javascript
ExtJS4如何给同一个formpanel不同的url
2014/05/02 Javascript
node.js中的http.response.setHeader方法使用说明
2014/12/14 Javascript
js Calender控件使用详解
2015/01/05 Javascript
jQuery固定元素插件scrolltofixed使用指南
2015/04/21 Javascript
jquery实现仿新浪微博评论滚动效果
2015/08/06 Javascript
浅析JavaScript回调函数应用
2016/05/22 Javascript
vue中axios处理http发送请求的示例(Post和get)
2017/10/13 Javascript
js实现无缝滚动双图切换效果
2019/07/09 Javascript
使用ThinkJs搭建微信中控服务的实现方法
2019/08/08 Javascript
vue实现表格过滤功能
2019/09/27 Javascript
Node.js API详解之 V8模块用法实例分析
2020/06/05 Javascript
Vue-cli4 配置 element-ui 按需引入操作
2020/09/11 Javascript
Python中用memcached来减少数据库查询次数的教程
2015/04/07 Python
在Python中使用成员运算符的示例
2015/05/13 Python
Python 多线程实例详解
2017/03/25 Python
Python3 中作为一等对象的函数解析
2019/12/11 Python
使用纯 CSS 创作一个脉动 loader效果的源码
2018/09/28 HTML / CSS
Mio Skincare中文官网:肌肤和身体护理
2016/10/26 全球购物
英国和世界各地鲜花速递专家:Arena Flowers
2018/02/10 全球购物
英国计算机商店:Technextday
2019/12/28 全球购物
香奈儿美国官网:CHANEL美国
2020/05/20 全球购物
汉语言文学毕业生求职信
2013/10/01 职场文书
历史教育专业个人求职信
2013/12/13 职场文书
安全生产汇报材料
2014/02/17 职场文书
法人单位授权委托书范文
2014/10/06 职场文书
2015年五一劳动节活动总结
2015/02/09 职场文书
2016年“我们的节日·重阳节”主题活动总结
2016/04/01 职场文书
《孙子兵法》:欲成大事者,需读懂这些致胜策略
2019/08/23 职场文书
Pycharm 如何设置HTML文件自动补全代码或标签
2021/05/21 Python
为什么MySQL 删除表数据 磁盘空间还一直被占用
2021/10/16 MySQL