string.match(RegExp) 与 RegExp.exec(string) 深入详解
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) 深入详解的更多相关文章
- js字符串和正则表达式中的match、replace、exec等函数详解
正则并不是经常使用,而正则和字符串之间的函数关系又错综复杂,谁是谁的函数,又是怎么样的一种结果,往往我们是看一遍忘一遍,对此我是头疼不已,感觉自己是个笨蛋^_^. 为了以后不再查文档,特此把常用的函数 ...
- WScript.Shell对象的 run()和exec()函数使用详解
WScript.Shell对象的 run()和exec()函数使用详解 http://blog.sina.com.cn/s/blog_6e14a2050102v47g.html vbScript ...
- java中String类、StringBuilder类和StringBuffer类详解
本位转载自http://www.cnblogs.com/dolphin0520/p/3778589.html 版权声明如下: 作者:海子 出处:http://www.cnblogs.com/dolp ...
- 不可变字符串String与可变字符串StringBuilder、StringBuffer使用详解
String字符串 char类型只能表示一个字符,而String可以表示字符串,也就是一个字符序列.但String不是基本类型,而是一个定义好的类,是一个引用类型.在Java中,可以将字符串直接量赋给 ...
- Java 字符串比较,String 中的一些方法 == 和 equals 的详解
"==" 是比较的是两个对象的内存地址,而equals方法默认情况下是比较两个对象的内存地址. 1.String str = "hello" 生成的字符串,首 ...
- 执行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 ...
- Animator.SetFloat(string name,float value,float dampTime,float deltaTime)详解
一般来说,我们用到的是这个API: animator.SetFloat("Speed",2.0f); 但是这个还有一个重载的方法,叫做: Animator.SetFloat(str ...
- 【Linux 进程】exec族函数详解
exec族的组成: 在Linux中,并不存在一个exec()的函数形式,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **e ...
- python的exec、eval详解
exec exec语句用来执行储存在字符串或文件中的Python语句.例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用exec语句执行这些语句.下面是一个简单的例子. exec ' ...
- exec族函数详解及循环创建子进程
前言:之前也知道exec族函数,但没有完全掌握,昨天又重新学习了一遍,基本完全掌握了,还有一些父子进程和循环创建子进程的问题,还要介绍一下环境变量,今天分享一下. 一.环境变量 先介绍下环境的概念和特 ...
随机推荐
- [笔记]CodeIgniter的SESSION
由于HTTP协议本身是无状态的,所以当保留某个用户的访问状态信息时,需要客户端有一个唯一标识传给服务端,这个唯一标识就是SESSION ID,存放在客户端的COOKIE中,然后服务端根据该标 ...
- scanf()与gets()的区别
scanf()与gets()的区别 1.scanf()可以同时接受多个字符串,而gets()一次只能接受一个字符串. #include<stdio.h>int main(){ char s ...
- oracle Union 中 ORA-12704:字符集不匹配问题的解决 .
在使用Union all连接时,若A集合中某列为nvarchar2或nvarchar类型,而B集合中无此列,用‘ ’ 来代替是会报字符集不匹配,解决方法有两种,见下面的示例 例: select '中国 ...
- Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7
Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7 By SK - August 12, 201 ...
- Servlet技术基础
由于Servlet部分涉及较多的类,要想尽快掌握Servlet基础,必须熟悉使用这些类之间的关系以及其常用的方法. 主要讲解部分包括: 1)通过实现Servelt接口来编写Servlet 2)熟悉Se ...
- Kubernetes才是微服务和DevOps的桥梁
一.从企业上云的三大架构看容器平台的三种视角 一切都从企业上云的三大架构开始. 如图所示,企业上的三大架构为IT架构,应用架构和数据架构,在不同的公司,不同的人,不同的角色,关注的重点不同. 对于大部 ...
- mschart 使用心得和部署。
参考: http://www.cnblogs.com/suguoqiang/archive/2013/01/16/2862945.html 1.在统计时可能需要多条数据,需要整合数据源 Chart1. ...
- PDB文件:每个开发人员都必须知道的 PDB Files
PDB文件:每个开发人员都必须知道的 PDB Files: What Every Developer Must Knowhttp://www.wintellect.com/CS/blogs/jro ...
- erlang otp中的socket参数设置
抄自http://www.zackzod.me/2012/10/24/socket-options-in-erlang-otp.html Erlang的inet模块里提供了对Socket进行一系列参数 ...
- web攻击之一:XSS跨站脚本
一.浏览器安全 同源策略 影响源的因素:host,子域名,端口,协议 a.com通过以下代码: <script scr=http://b.com/b.js> 加载了b.com上的b.js, ...