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族函数,但没有完全掌握,昨天又重新学习了一遍,基本完全掌握了,还有一些父子进程和循环创建子进程的问题,还要介绍一下环境变量,今天分享一下. 一.环境变量 先介绍下环境的概念和特 ...
随机推荐
- JFreeChart的简单使用
实例1:简单的饼图 public class Test { public static void main(String[] args) { //建立默认的饼图 DefaultPieDataset d ...
- vi/vim使用进阶: 在VIM中使用GDB调试 – 使用vimgdb
vi/vim使用进阶: 在VIM中使用GDB调试 – 使用vimgdb << 返回vim使用进阶: 目录 本节所用命令的帮助入口: :help vimgdb 在UNIX系统最初设计时,有一 ...
- JSON用法之将PHP数组转JS数组,JS如何接收PHP数组
先看php文件,当我们获取到$arr这个数组后 foreach ($arr as $value) { $json .= json_encode($value) . ','; } echo '[' . ...
- Linux中epoll+线程池实现高并发
服务器并发模型通常可分为单线程和多线程模型,这里的线程通常是指“I/O线程”,即负责I/O操作,协调分配任务的“管理线程”,而实际的请求和任务通常交由所谓“工作者线程”处理.通常多线程模型下,每个线程 ...
- linux shell获取用户输入
一.获取用户输入1.基本读取read命令接收标准输入的输入,或其它文件描述符的输入.得到输入后,read命令将数据输入放入一个标准变量中.[root@rac2 ~]# cat t8.sh #!/bin ...
- 解决:win8.1 oepnvpn客户端 redirect-gateway def1无效,自动获取的IP没有网关问题
解决:win8.1 oepnvpn客户端 redirect-gateway def1无效,自动获取的IP没有网关问题 该问题是操作系统权限问题,需要将程序设置为以管理员模式运行和以windows7兼容 ...
- ASP.NET 迭代控件获得行号
如何获取Repeater的当前行号,其实Repeater自身就带有这个获取当前行号的属性,而无需程序员绑定这个行号.到底要怎么实现呢?其实使用Repeater中的 Container.ItemInde ...
- Excel VBA 找出选定范围不重复值和重复值
Sub 找出选定范围内不重复的值() On Error Resume Next Dim d As Object Set d = CreateObject("scripting.diction ...
- OSG和ProLand 的海面仿真
基于OSG的海面仿真 OSG中国官网 http://www.osgchina.org/ OSG-ocean的效果图如下 proland的效果图如下 下面为OSG和OCEAN的配置 配置方法转自 htt ...
- Tomcat中部署网站和绑定域名
在安装的tomcat的文件夹下有个conf文件夹 下面有个server.xml文件, 1. 使用80端口 默认tomcat用的是8080端口. <Connector port="808 ...