博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RegExp 对象 (JavaScript)
阅读量:6896 次
发布时间:2019-06-27

本文共 2657 字,大约阅读时间需要 8 分钟。

$1...$9 属性 (RegExp) (JavaScript)

返回在模式匹配期间找到的,所存储的最近的九个部分。只读。

 
 
 
 
RegExp.$n
 
 
RegExp

始终为全局 RegExp 对象。

n

1 至 9 之间的任意整数。

 
 

每当产生一个带括号的成功匹配时,$1...$9 属性的值就被修改。可以在一个正则表达式模式中指定任意多个带括号的子匹配,但只能存储最新的九个。

下面的示例执行正则表达式搜索。它显示了全局 RegExp 对象中的匹配项和子匹配项。子匹配项是 $1…$9 属性中包含的成功的带括号匹配项。该示例还显示了由 exec 方法返回的数组中的匹配项和子匹配项。

var newLine = "
";var re = /(\w+)@(\w+)\.(\w+)/gvar src = "Please send mail to george@contoso.com and someone@example.com. Thanks!"var result;var s = "";// Get the first match.result = re.exec(src);while (result != null) { // Show the entire match. s += newLine; // Show the match and submatches from the RegExp global object. s += "RegExp.lastMatch: " + RegExp.lastMatch + newLine; s += "RegExp.$1: " + RegExp.$1 + newLine; s += "RegExp.$2: " + RegExp.$2 + newLine; s += "RegExp.$3: " + RegExp.$3 + newLine; // Show the match and submatches from the array that is returned // by the exec method. for (var index = 0; index < result.length; index++) { s += index + ": "; s += result[index]; s += newLine; } // Get the next match. result = re.exec(src);}document.write(s);// Output:// RegExp.lastMatch: george@contoso.com// RegExp.$1: george// RegExp.$2: contoso// RegExp.$3: com// 0: george@contoso.com// 1: george// 2: contoso// 3: com// RegExp.lastMatch: someone@example.com// RegExp.$1: someone// RegExp.$2: example// RegExp.$3: com// 0: someone@example.com// 1: someone// 2: example// 3: com

index 属性 (RegExp) (JavaScript)

返回字符位置,它是被搜索字符串中第一个成功匹配的开始位置。只读。

 
 
 
 
RegExp.index
 
 

与此属性关联的对象始终为全局 RegExp 对象。

index 属性是从零开始的。 index 属性的初始值是 –1。每当产生成功匹配时,其值就会相应更改。

下面的示例阐释了 index 属性的用法。该函数重复一个字符串搜索,并打印出字符串中每一个词的 index 和 lastIndex 值。

function RegExpTest(){   var ver = Number(ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion())   if (ver < 5.5)   {      document.write("You need a newer version of JavaScript for this to work");      return;   }   var src = "The quick brown fox jumps over the lazy dog.";   // Create regular expression pattern with a global flag.   var re = /\w+/g;   // Get the next word, starting at the position of lastindex.   var arr;   while ((arr = re.exec(src)) != null)      {      // New line:      document.write ("
"); document.write (arr.index + "-" + arr.lastIndex + " "); document.write (arr); }}

input 属性 ($_) (RegExp) (JavaScript)

返回执行正则表达式搜索所针对的字符串。只读。

 
 
 
 
RegExp.input
 
 

与此属性关联的对象始终为全局 RegExp 对象。

只要搜索字符串发生更改,就可以修改 input 属性的值。

下面的示例阐释了 input 属性的用法:

function inputDemo(){   var s;   var re = new RegExp("d(b+)(d)","ig");   var str = "cdbBdbsbdbdz";   var arr = re.exec(str);   s = "The string used for the match was " + RegExp.input;    return(s);}

 

转载地址:http://alddl.baihongyu.com/

你可能感兴趣的文章
第二、UIScrollView的使用大全
查看>>
Ehcache(03)——Ehcache中储存缓存的方式
查看>>
基于海康监控的图像识别设计
查看>>
Senparc.Weixin.MP SDK 微信公众平台开发教程(十五):消息加密
查看>>
CCNA2.0笔记_HSRP
查看>>
wamp You don't have permission to access / on this server等问题的解决.
查看>>
【POJ】3071 Football
查看>>
ArcGis 统计方法
查看>>
Resource interpreted as Script but transferred with MIME type text/plain
查看>>
VirtualBox安装及使用说明和虚拟机安装XP系统图文教程
查看>>
【android】优秀的UI资源站点集合
查看>>
iOS 容易引“起循环引用”的三种场景
查看>>
轮播图点击 手势 代码
查看>>
新年札记:自学系统补完计划
查看>>
[Javascript] Gradient Fills on the HTML5 Canvas
查看>>
对CAB文件进行数字签名
查看>>
SQL Server 变更数据捕获(CDC)
查看>>
Starting httpd:Could not reliably determine the server's fully qualified domain name
查看>>
2015第19周一
查看>>
datatables 参数详解(转)
查看>>