string.match(RegExp) 与 RegExp.exec(string) 相同点与不同点对比解析:

1. 这两个方法,如果匹配成功,返回一个数组,匹配失败,返回null。

2. 当RegExp的global属性为false时,这两个方法的返回数组是一样的。

数组的第0个元素是整个str的第一个匹配字符串,接下来的元素是str第一个匹配中的子匹配字符串。

  此外,数组还有index和input两个额外属性,index是匹配字符串的起始位置,input是整个输入字符串。

  此时,RegExp的lastIndex属性一直是0(可为什么跟下面测试结果不一样呢?)。

实例01(不带g标识符):

 <script type="text/JavaScript">                                    

 var str="this is a string";                                        

 var reg=/\b\w*(i)s\b/;                                             

 var rm=str.match(reg);                                             

 var re=reg.exec(str);                                              

 document.write("string.match(RegExp)测试结果:<br\>");             

 document.write("string.match(RegExp)返回数组:"+rm+"<br\>");       

 document.write("string.match(RegExp).index:"+rm.index+"<br\>");    

 document.write("string.match(RegExp).input:"+rm.input+"<br\>");

 document.write("string.match(RegExp).lastIndex:"+reg.lastIndex+"<br\>");    

 document.write("===============================<br\>");            

 document.write("RegExp.exec(string)测试结果:<br\>");              

 document.write("RegExp.exec(string)返回数组:"+re+"<br\>");        

 document.write("RegExp.exec(string).index:"+re.index+"<br\>");    

 document.write("RegExp.exec(string).input:"+re.input+"<br\>");    

 document.write("RegExp.exec(string).lastIndex:"+reg.lastIndex+"<br\>");    

 </script>   

 输出结果:

 string.match(RegExp)测试结果:

 string.match(RegExp)返回数组:this,i

 string.match(RegExp).index:0

 string.match(RegExp).input:this is a string

 string.match(RegExp).lastIndex:4

 ===============================

 RegExp.exec(string)测试结果:

 RegExp.exec(string)返回数组:this,i

 RegExp.exec(string).index:0

 RegExp.exec(string).input:this is a string

 RegExp.exec(string).lastIndex:4

代码01

3. 当RegExp的global属性为true时,返回的数组是不同的。

  match()方法返回的数组包含着所有匹配字符串,没有子匹配字符串和额外属性(为什么下面实际测试是有index和input额外属性的呢?而且index的数值是最后一个匹配字符串的位置?)。此时,lastIndex属性无效。

  exec()方法返回的数组格式与global为false时一样,只是此时RegExp的lastIndex属性有效,匹配是从lastIndex所指示的字符开始的,并且方法执行后会将lastIndex置为本次匹配

  字符串的下一个字符处,所以循环执行exec方法时会依次匹配整个字符串,直到字符串最后返回null,并将lastIndex置0。

注:下面的测试代码必须设置两个RegExp变量(reg1,reg2),否则re始终为null,while(){}循环内部始终进不去,至于原因,暂时不知道!!!!!!

实例02(带g标识符):

 <script type="text/JavaScript">
var str="this is a string";
var reg1=/\b\w*(i)s\b/g;
var reg2=/\b\w*(i)s\b/g;
var rm = str.match(reg1);
var re;
document.write("string.match(RegExp)带全局变量g 测试结果:<br\>");
document.write("string.match(RegExp)返回数组:"+rm+"<br\>");
document.write("string.match(RegExp).index:"+rm.index+"<br\>");
document.write("string.match(RegExp).input:"+rm.input+"<br\>");
document.write("string.match(RegExp).lastIndex:"+reg1.lastInde+"<br\>"); document.write("==========================================<br\>");
document.write("RegExp.exec(string)带全局变量g 测试结果:<br\>");
while(re=reg2.exec(str))
{
document.write("RegExp.exec(string)返回数组:"+re+"<br\>");
document.write("RegExp.exec(string).index:"+re.index+"<br\>");
document.write("RegExp.exec(string).input:"+re.input+"<br\>");
document.write("RegExp.exec(string).lastIndex:"+reg2.lastIndex+"<br\>");
document.write("----------------------------------------<br\>"); }
document.write("RegExp.exec(string)循环完成后返回数组:"+re+"<br\>");
document.write("RegExp.exec(string).lastIndex:"+reg2.lastIndex+"<br\>"); </script> 输出结果: string.match(RegExp)带全局变量g 测试结果:
string.match(RegExp)返回数组:this,is
string.match(RegExp).index:5
string.match(RegExp).input:this is a string
string.match(RegExp).lastIndex:undefined
==========================================
RegExp.exec(string)带全局变量g 测试结果:
RegExp.exec(string)返回数组:this,i
RegExp.exec(string).index:0
RegExp.exec(string).input:this is a string
RegExp.exec(string).lastIndex:4
----------------------------------------
RegExp.exec(string)返回数组:is,i
RegExp.exec(string).index:5
RegExp.exec(string).input:this is a string
RegExp.exec(string).lastIndex:7
----------------------------------------
RegExp.exec(string)循环完成后返回数组:null
RegExp.exec(string).lastIndex:0

代码02

综上:
1.在没有g标识符时,match和exec方法效果是一样的;有g标识符时,exec方法可以提供最完整的匹配结果。
2.这里顺便提一下RegExp.test()方法,它是exec方法的简化版,有匹配结果就返回true,没有匹配结果就返回false,执行过程与exec是一样的。相当于 (p.exec(s) != null)。
3.RegExp的lastIndex属性在有g标识符,且在exec和test方法中是有效的,其他地方是无效的(可实际上在string.match(RegExp)不带g标识符的方法中,也是有效的,参看上面 代码01)。

最新文章

  1. lumen 构建api(dingo api)
  2. 好久没上cnblogs了
  3. Linux环境下使用shell编写CGI(httpd)
  4. Java开发高薪之路__大纲篇
  5. 【转】Android 获得view的宽和高
  6. dede如何实现二级栏目导航的仿制
  7. 【转】C语言中标识符的作用域、命名空间、链接属性、生命周期、存储类型
  8. php 短信网关短信内容不能有空格
  9. XX cannot be resolved to a type
  10. Cmpletepack coming~^.^
  11. Nagios+pnp4nagios+rrdtool 安装配置为nagios添加自定义插件(三)
  12. 对 Linux 新手有用的 20 个命令
  13. Nginx+IIS+Redis 处理Session共享问题 2
  14. 在CentOS7中安装.Net Core2.0 SDK
  15. MySQL - 扩展性 3 负载均衡:眼花缭乱迷人眼
  16. MODBUS协议解析中常用的转换帮助类(C#)
  17. php,js 对字符串按位异或运算加密解密
  18. 24-hadoop-hiveserver2&amp;jdbc-正则数据导入
  19. spring boot 学习(七)小工具篇:表单重复提交
  20. ASM Disk Discovery 最佳实践

热门文章

  1. java第十次面试题
  2. Ztree小demo用于系统授权
  3. IO流常规操作
  4. cratedb 集群 docker-compose 安装试用
  5. FastAdmin 的前端环境怎么安装?
  6. 再看Spring Could微服务的关键组件
  7. 自动下载google reader里面的星标文章
  8. UEFI +、GPT 、BIOS 、 MBR的关系
  9. 《快学Scala》
  10. 在NOILINUX下的简易VIM配置