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 相关文章推荐
新闻内页-JS分页
Jun 07 Javascript
js改变文章字体大小的实例代码
Nov 27 Javascript
js生成随机数之random函数随机示例
Dec 20 Javascript
JS使用正则表达式除去字符串中重复字符的方法
Nov 05 Javascript
基于javascript实现listbox左右移动
Jan 29 Javascript
很酷的星级评分系统原生JS实现
Aug 25 Javascript
Bootstrap Navbar Component实现响应式导航
Oct 08 Javascript
JS Input里添加小图标的两种方法
Nov 11 Javascript
使用vue-router beforEach实现判断用户登录跳转路由筛选功能
Jun 25 Javascript
JSON基本语法及与JavaScript的异同实例分析
Jan 04 Javascript
vue v-for循环重复数据无法添加问题解决方法【加track-by='索引'】
Mar 15 Javascript
vue中beforeRouteLeave实现页面回退不刷新的示例代码
Nov 01 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
关于url地址传参数时字符串有回车造成页面脚本赋值失败的解决方法
2013/06/28 PHP
PHP遍历目录函数opendir()、readdir()、closedir()、rewinddir()总结
2014/11/18 PHP
php实现多维数组中每个单元值(数字)翻倍的方法
2015/02/16 PHP
php中return的用法实例分析
2015/02/28 PHP
ucenter中词语过滤原理分析
2016/07/13 PHP
遍历指定目录,并存储目录内所有文件属性信息的php代码
2016/10/28 PHP
JQuery的ajax基础上的超强GridView展示
2009/09/18 Javascript
JS关键字变色实现思路及代码
2013/02/21 Javascript
学习JavaScript设计模式(接口)
2015/11/26 Javascript
利用HTML5+Socket.io实现摇一摇控制PC端歌曲切换
2017/01/13 Javascript
简单实现js无缝滚动效果
2017/02/05 Javascript
微信小程序-获得用户输入内容
2017/02/13 Javascript
纯JS实现只能输入数字的简单代码
2017/06/21 Javascript
Angular2实现组件交互的方法分析
2017/12/19 Javascript
vue采用EventBus实现跨组件通信及注意事项小结
2018/06/14 Javascript
layui监听单元格编辑前后交互的例子
2019/09/16 Javascript
jquery选择器和属性对象的操作实例分析
2020/01/10 jQuery
easy_install python包安装管理工具介绍
2013/02/10 Python
python生成随机密码或随机字符串的方法
2015/07/03 Python
Python使用matplotlib实现基础绘图功能示例
2018/07/03 Python
Django Rest framework之认证的实现代码
2018/12/17 Python
Django CBV与FBV原理及实例详解
2019/08/12 Python
Python模拟FTP文件服务器的操作方法
2020/02/18 Python
python 日志 logging模块详细解析
2020/03/31 Python
HMV日本官网:全球知名的音乐、DVD和电脑游戏零售巨头
2016/08/13 全球购物
Happy Socks英国官网:购买五颜六色的袜子
2020/11/03 全球购物
舞蹈毕业生的自我评价
2014/03/05 职场文书
优秀护士先进事迹
2014/05/08 职场文书
办公楼租房协议书范本
2014/11/25 职场文书
师德先进个人事迹材料
2014/12/19 职场文书
2015高考寄语集锦
2015/02/27 职场文书
2015政治思想表现评语
2015/03/25 职场文书
会计试用期工作总结2015
2015/05/28 职场文书
《比的意义》教学反思
2016/02/18 职场文书
Vue.js 带下拉选项的输入框(Textbox with Dropdown)组件
2021/04/17 Vue.js
Python 文字识别
2022/05/11 Python