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)。

string.match(RegExp) 与 RegExp.exec(string) 深入详解的更多相关文章

  1. js字符串和正则表达式中的match、replace、exec等函数详解

    正则并不是经常使用,而正则和字符串之间的函数关系又错综复杂,谁是谁的函数,又是怎么样的一种结果,往往我们是看一遍忘一遍,对此我是头疼不已,感觉自己是个笨蛋^_^. 为了以后不再查文档,特此把常用的函数 ...

  2. WScript.Shell对象的 run()和exec()函数使用详解

    WScript.Shell对象的 run()和exec()函数使用详解 http://blog.sina.com.cn/s/blog_6e14a2050102v47g.html   vbScript ...

  3. java中String类、StringBuilder类和StringBuffer类详解

    本位转载自http://www.cnblogs.com/dolphin0520/p/3778589.html  版权声明如下: 作者:海子 出处:http://www.cnblogs.com/dolp ...

  4. 不可变字符串String与可变字符串StringBuilder、StringBuffer使用详解

    String字符串 char类型只能表示一个字符,而String可以表示字符串,也就是一个字符序列.但String不是基本类型,而是一个定义好的类,是一个引用类型.在Java中,可以将字符串直接量赋给 ...

  5. Java 字符串比较,String 中的一些方法 == 和 equals 的详解

    "==" 是比较的是两个对象的内存地址,而equals方法默认情况下是比较两个对象的内存地址. 1.String str = "hello"  生成的字符串,首 ...

  6. 执行Hive时出现org.apache.hadoop.util.RunJar.main(RunJar.java:136) Caused by: java.lang.NumberFormatException: For input string: "1s"错误的解决办法(图文详解)

    不多说,直接上干货 问题详情 [kfk@bigdata-pro01 apache-hive--bin]$ bin/hive Logging initialized -bin/conf/hive-log ...

  7. Animator.SetFloat(string name,float value,float dampTime,float deltaTime)详解

    一般来说,我们用到的是这个API: animator.SetFloat("Speed",2.0f); 但是这个还有一个重载的方法,叫做: Animator.SetFloat(str ...

  8. 【Linux 进程】exec族函数详解

    exec族的组成: 在Linux中,并不存在一个exec()的函数形式,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **e ...

  9. python的exec、eval详解

    exec exec语句用来执行储存在字符串或文件中的Python语句.例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用exec语句执行这些语句.下面是一个简单的例子. exec ' ...

  10. exec族函数详解及循环创建子进程

    前言:之前也知道exec族函数,但没有完全掌握,昨天又重新学习了一遍,基本完全掌握了,还有一些父子进程和循环创建子进程的问题,还要介绍一下环境变量,今天分享一下. 一.环境变量 先介绍下环境的概念和特 ...

随机推荐

  1. soql取第一件数据

    User u = [select ID,Name from User Limit 1];

  2. python 保存文件时候, 去除名字中的非法字符

    import re def validateTitle(title): rstr = r"[\/\\\:\*\?\"\<\>\|]" # '/ \ : * ? ...

  3. proc 文件系统学习

    proc.txt翻译 ------------------------------------------------------------------------------Version 1.3 ...

  4. QLoo graphql engine了解

    参考架构图 处理流程 使用gloo注册服务api 发现断电以及serverless 函数 更新graphql schema 在qloo的resolvermap 中连接schema定义的字段 特性 不用 ...

  5. hasura graphql auth-webhook api 说明

    hasura graphql 生产的使用是推荐使用webhook 进行角色访问控制的,官方同时提供了一个nodejs 的简单demo 代码 git clone https://github.com/h ...

  6. 【转】VC++10(VS2010)IDE各种使用技巧

    原文网址:http://www.cnblogs.com/sunrisezhang/articles/2802397.html 一个好的coder,他首先必须是一个熟练工.对于C++程序员来说,只有掌握 ...

  7. git回滚分支版本到指定版本

    昨天提交代码时Eclipse凌乱了,本来拉了dev-20190201分支的,结果提交时竟然跑到dev分支了.为了把dev分支回滚,可以有两种方式:Eclipse和命令行. 先说简单的命令行方式,先用g ...

  8. Mysql向存储过程中传递中文参数变成乱码的解决方案

    今天做程序需要用到一个存储过程,然后用php程序调用.  存储过程如下: delimiter $$ CREATE PROCEDURE disagree_upgrade_detail(a int,b t ...

  9. 完全卸载vs2013、vs2015的方法

    Visual Studio安装过程会安装好多组件,如果想要卸载的话会出现一些因难,在控制面板不容易卸载干净,在Linux下的命令都有--help参数来显示命令的用法,今天突发奇想,在控制台下输入vs2 ...

  10. WIN10下搭建react-native开发Android环境

    最近公司要求使用react-native进行移动端开发,据说macOS上开发坑会少的多,但我们是windows,莫法,直接抗吧!周末配置环境遇到很多问题,谨以此文做个记录... 准备 安装Chocol ...