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. vue-cl发布vue

    npm run dev是开发环境, npm run build是生产环境, 在开发环境完成代码和测试, 之后用生产环境生成代码, npm run build的时候, 一开始就会提示Built file ...

  2. 设计模式(Python)-单例模式

    本系列文章是希望将软件项目中最常见的设计模式用通俗易懂的语言来讲解清楚,并通过Python来实现,每个设计模式都是围绕如下三个问题: 为什么?即为什么要使用这个设计模式,在使用这个模式之前存在什么样的 ...

  3. fusionjs 学习一 基本试用

    参考demo 项目 https://github.com/rongfengliang/fusionjs-docker-demo 安装 create startkit yarn global add c ...

  4. C#多线程应用:子线程更新主窗体控件的值(二)

    在上篇文章中,我已经给大家列了一个在主线程中实现的方式,这篇文章来给大家说说使用Invoke的方式的例子: 对于不代理不太熟悉的朋友,建议先查查相关资料: 例子一: 在C#中,直接在子线程中对窗体上的 ...

  5. Cocos2d-x调用Java 代码

    Java代码: package com.dishu; import com.dishu.org.R; import android.app.Activity; import android.app.A ...

  6. VS2017 Linux C++引用自定义的动态库

    前一篇博客讲了用系统库libpthread.so的例子,只需要在项目属性页的[C++->命令行参数]和[链接器->命令行参数]中加上对应参数(比如-pthread)即可,然后我试着引用自己 ...

  7. mac电脑安装selenium 记录

    1.使用终端去命令安装 sudo easy_install selenium 参考:https://www.cnblogs.com/nichoc/p/5543654.html 2.听说驱动放在 /us ...

  8. 这个移动通讯中 DB 、DBm 、瓦的基本知识的问题:

    1.对于无线工程师来说更常用分贝dBm这个单位,dBm单位表示相对于1毫瓦的分贝数,dBm和W之间的关系是:dBm=10*lg(mW)1w的功率,换算成dBm就是10×lg1000=30dBm.2w是 ...

  9. servlet的登陆案例

    Users.java package com.po; public class Users { private String username; private String password; pu ...

  10. 简单的自动化测试模型(python+selenium)

             刚接触自动化测试,由于没有编程语言的基础,是搞不懂代码里面的函数.封装.包以及其他概念,只是了解字符串.数组.元组及字典这种最基本的名词,更不懂自动化测试框架了.          ...