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 相关文章推荐
运用jquery实现table单双行不同显示并能单行选中
Jul 25 Javascript
js和jquery批量绑定事件传参数一(新猪猪原创)
Jun 23 Javascript
jQuery选择器querySelector的使用指南
Jan 23 Javascript
jQuery插件AjaxFileUpload实现ajax文件上传
May 05 Javascript
Jquery Easyui验证组件ValidateBox使用详解(20)
Dec 18 Javascript
微信小程序 Template详解及简单实例
Jan 05 Javascript
js实现导航吸顶效果
Feb 24 Javascript
浅谈Vue.js 1.x 和 2.x 实例的生命周期
Jul 25 Javascript
Vue2.0 实现移动端图片上传功能
May 30 Javascript
vue组件数据传递、父子组件数据获取,slot,router路由功能示例
Mar 19 Javascript
Layui实现带查询条件的分页
Jul 27 Javascript
ant-design-vue 时间选择器赋值默认时间的操作
Oct 27 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
Ajax PHP分页演示
2007/01/02 PHP
PHP array_multisort() 函数的深入解析
2013/06/20 PHP
ThinkPHP实现更新数据实例详解(demo)
2016/06/29 PHP
PHP析构函数destruct与垃圾回收机制的讲解
2019/03/22 PHP
PHP实现带进度条的Ajax文件上传功能示例
2019/07/02 PHP
from 表单提交返回值用post或者是get方法实现
2013/08/21 Javascript
js导出table数据到excel即导出为EXCEL文档的方法
2013/10/10 Javascript
原生js做的手风琴效果的导航菜单
2013/11/08 Javascript
YUI模块开发原理详解
2013/11/18 Javascript
JavaScript前端图片加载管理器imagepool使用详解
2014/12/29 Javascript
深入理解JavaScript系列(50):Function模式(下篇)
2015/03/04 Javascript
JS与jQuery实现隔行变色的方法
2016/09/09 Javascript
基于Phantomjs生成PDF的实现方法
2016/11/07 Javascript
原生JS轮播图插件
2017/02/09 Javascript
详解VueJs前后端分离跨域问题
2017/05/24 Javascript
Vue.js+cube-ui(Scroll组件)实现类似头条效果的横向滚动导航条
2019/06/24 Javascript
js实现旋转木马轮播图效果
2020/01/10 Javascript
vue项目中使用bpmn为节点添加颜色的方法
2020/04/30 Javascript
[02:06]DOTA2英雄基础教程 暗影萨满
2013/12/16 DOTA
用Python编写生成树状结构的文件目录的脚本的教程
2015/05/04 Python
Python中多个数组行合并及列合并的方法总结
2018/04/12 Python
Pycharm 2020最新永久激活码(附最新激活码和插件)
2020/09/17 Python
Python xlrd模块导入过程及常用操作
2020/06/10 Python
python如何控制进程或者线程的个数
2020/10/16 Python
瑞典耳机品牌:URBANISTA
2019/12/03 全球购物
护士实习生自我鉴定范文
2013/12/10 职场文书
电脑租赁公司创业计划书
2014/01/08 职场文书
迟到检讨书800字
2014/01/13 职场文书
出国签证在职证明
2014/01/16 职场文书
xxx同志考察材料
2014/02/07 职场文书
幼儿园中班开学寄语
2014/04/03 职场文书
2014年清明节网上祭英烈寄语
2014/04/09 职场文书
优秀党务工作者先进事迹材料
2014/12/25 职场文书
幼师辞职信范文
2015/02/27 职场文书
2015年体检中心工作总结
2015/05/27 职场文书
2015年秋季小班开学寄语
2015/05/27 职场文书