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 无废话系列教程(一) jquery入门 [推荐]
Jun 23 Javascript
JavaScript+html5 canvas制作的圆中圆效果实例
Jan 27 Javascript
AngularJS基础 ng-click 指令示例代码
Aug 01 Javascript
浅谈js键盘事件全面控制
Dec 01 Javascript
Vue数据驱动模拟实现1
Jan 11 Javascript
微信小程序 自定义Toast实例代码
Jun 12 Javascript
vue获取dom元素注意事项
Dec 28 Javascript
爬虫利器Puppeteer实战
Jan 09 Javascript
详解Vue、element-ui、axios实现省市区三级联动
May 07 Javascript
vue通过video.js解决m3u8视频播放格式的方法
Jul 30 Javascript
JavaScript实现PC端横向轮播图
Feb 07 Javascript
JS实现页面炫酷的时钟特效示例
Aug 14 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中使用Curl、socket、file_get_contents三种方法POST提交数据
2011/08/12 PHP
Yii2数据库操作常用方法小结
2017/05/04 PHP
Laravel框架生命周期与原理分析
2018/06/12 PHP
PHP中命名空间的使用例子
2019/03/22 PHP
实现点击列表弹出列表索引的两种方式
2013/03/08 Javascript
关于JQuery($.load)事件的用法和分析
2013/04/09 Javascript
Jquery中"$(document).ready(function(){ })"函数的使用详解
2013/12/30 Javascript
Javascript中this的用法详解
2014/09/22 Javascript
TinyMCE提交AjaxForm获取不到数据的解决方法
2015/03/05 Javascript
Javascript中typeof 用法小结
2015/05/12 Javascript
jQuery向webApi提交post json数据
2017/01/16 Javascript
解决vue处理axios post请求传参的问题
2018/03/05 Javascript
详解vue 单页应用(spa)前端路由实现原理
2018/04/04 Javascript
React native ListView 增加顶部下拉刷新和底下点击刷新示例
2018/04/27 Javascript
webpack4 optimization使用总结
2019/11/10 Javascript
如何手写一个简易的 Vuex
2020/10/10 Javascript
WebPack工具运行原理及入门教程
2020/12/02 Javascript
[01:50]《我与DAC》之玩家:iG夺冠时的那面红旗
2018/03/29 DOTA
[04:16]DOTA2全国高校联赛16强抽签
2018/05/02 DOTA
Python查询Mysql时返回字典结构的代码
2012/06/18 Python
在IIS服务器上以CGI方式运行Python脚本的教程
2015/04/25 Python
自己使用总结Python程序代码片段
2015/06/02 Python
python从入门到精通(DAY 2)
2015/12/20 Python
Python常见MongoDB数据库操作实例总结
2018/07/24 Python
python继承threading.Thread实现有返回值的子类实例
2020/05/02 Python
pip已经安装好第三方库但pycharm中import时还是标红的解决方案
2020/10/09 Python
纯CSS3实现移动端展开和收起效果的示例代码
2020/04/26 HTML / CSS
环境工程求职简历的自我评价范文
2013/10/24 职场文书
装修致歉信
2014/01/15 职场文书
运动会广播稿30字
2014/01/21 职场文书
办公室员工岗位工作职责
2014/03/10 职场文书
如何写早恋检讨书
2014/09/10 职场文书
教师见习报告范文
2014/11/03 职场文书
2014年环卫工作总结
2014/11/22 职场文书
2015年中秋节演讲稿
2015/03/20 职场文书
SpringBoot集成Druid连接池连接MySQL8.0.11
2021/07/02 Java/Android