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 相关文章推荐
TreeView 用法(有代码)(asp.net)
Jul 15 Javascript
FF IE浏览器修改标签透明度的方法
Jan 27 Javascript
JavaScript前补零操作实例
Mar 11 Javascript
jQuery控制Div拖拽效果完整实例分析
Apr 15 Javascript
浅谈Javascript线程及定时机制
Jul 02 Javascript
window.onload绑定多个事件的两种解决方案
May 15 Javascript
微信小程序 Windows2008 R2服务器配置TLS1.2方法
Dec 05 Javascript
关于HTTP传输中gzip压缩的秘密探索分析
Jan 12 Javascript
animate.css在vue项目中的使用教程
Aug 05 Javascript
vue+element+Java实现批量删除功能
Apr 08 Javascript
js找出5个数中最大的一个数和倒数第二大的数实现方法示例小结
Mar 04 Javascript
vue实现简单的登录弹出框
Oct 26 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学习 计数器实例代码
2008/06/15 PHP
php 删除数组元素
2009/01/16 PHP
php strstr查找字符串中是否包含某些字符的查找函数
2010/06/03 PHP
关于初学PHP时的知识积累总结
2013/06/07 PHP
PHP中常用的转义函数
2014/02/28 PHP
ThinkPHP3.1查询语言详解
2014/06/19 PHP
thinkphp微信开发(消息加密解密)
2015/12/02 PHP
jquery判断单个复选框是否被选中的代码
2009/09/03 Javascript
js动态添加onload、onresize、onscroll事件(另类方法)
2012/12/26 Javascript
nodejs分页类代码分享
2014/06/17 NodeJs
js中this用法实例详解
2015/05/05 Javascript
seajs加载jquery时提示$ is not a function该怎么解决
2015/10/23 Javascript
详解angularJs中关于ng-class的三种使用方式说明
2017/06/02 Javascript
vue实现某元素吸顶或固定位置显示(监听滚动事件)
2017/12/13 Javascript
vue实现简单loading进度条
2018/06/06 Javascript
jquery获取img的src值实例介绍
2019/01/16 jQuery
js中innerText/textContent和innerHTML与target和currentTarget的区别
2019/01/21 Javascript
JS合并两个数组的3种方法详解
2019/10/24 Javascript
Node.js控制台彩色输出的方法与原理实例详解
2019/12/01 Javascript
Vue 组件的挂载与父子组件的传值实例
2020/09/02 Javascript
React Ant Design树形表格的复杂增删改操作
2020/11/02 Javascript
使用Typescript开发微信小程序的步骤详解
2021/01/12 Javascript
详解Python中__str__和__repr__方法的区别
2015/04/17 Python
Python中的time模块与datetime模块用法总结
2016/06/30 Python
python+selenium开发环境搭建图文教程
2017/08/11 Python
Tensorflow设置显存自适应,显存比例的操作
2020/02/03 Python
关于html字符串正则判断和匹配的具体使用
2019/12/12 HTML / CSS
美国领先的户外服装与装备用品店:Moosejaw
2016/08/25 全球购物
小学三年级数学教学反思
2014/01/31 职场文书
大二法英学生职业生涯规划范文
2014/02/27 职场文书
法制宣传实施方案
2014/03/13 职场文书
检讨书1000字
2014/10/11 职场文书
装修公司工程部经理岗位职责
2015/04/09 职场文书
公司员工违纪检讨书
2015/05/05 职场文书
严以律己专题学习研讨会发言材料
2015/11/09 职场文书
CSS基础详解
2021/10/16 HTML / CSS