RegExp 对象 (JavaScript)
$1...$9 属性 (RegExp) (JavaScript)
返回在模式匹配期间找到的,所存储的最近的九个部分。只读。
RegExp.$n
- RegExp
-
始终为全局 RegExp 对象。
- n
-
1 至 9 之间的任意整数。
每当产生一个带括号的成功匹配时,$1...$9 属性的值就被修改。可以在一个正则表达式模式中指定任意多个带括号的子匹配,但只能存储最新的九个。
下面的示例执行正则表达式搜索。它显示了全局 RegExp 对象中的匹配项和子匹配项。子匹配项是 $1…$9 属性中包含的成功的带括号匹配项。该示例还显示了由 exec 方法返回的数组中的匹配项和子匹配项。
var newLine = "<br />"; var re = /(\w+)@(\w+)\.(\w+)/g
var 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 ("<br />");
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);
}
RegExp 对象 (JavaScript)的更多相关文章
- JS正则表达式从入门到入土(1)—— REGEXP对象
REGEXP对象 JavaScript通过内置对象RegExp支持正则表达式,有两种方法实例化RegExp对象. 1.字面量 2.构造函数 字面量 字面量是直接通过/.../创建RegExp对象实例. ...
- JavaScript RegExp 对象
JavaScript RegExp 对象 RegExp 对象用于规定在文本中检索的内容. 什么是 RegExp? RegExp 是正则表达式的缩写. 当您检索某个文本时,可以使用一种模式来描述要检索的 ...
- JavaScript RegExp对象
一.什么是RegExp 1.RegExp 是正則表達式的缩写. 2.当您检索某个文本时,能够使用一种模式来描写叙述要检索的内容.RegExp 就是这样的模式. 3.简单的模式能够是一个 ...
- Javascript的RegExp对象(转载自网络)
正则表达式是一个描述字符模式的对象. JavaScript的RegExp对象和String对象定义了使用正则表达式来执行强大的模式匹配和文本检索与替换函数的方法. '***************** ...
- JavaScript正则表达式(Regular Expression):RegExp对象
第一部分:新建正则表达式 JavaScript中正则表达式是参照Perl 5(一门历史很悠久的语言,现在tiobe编程语言排行依然在10名左右)建立的. 新建正则表达式的方法有两种: 1.使用字面量( ...
- JavaScript -- 时光流逝(六):js中的正则表达式 -- RegExp 对象
JavaScript -- 知识点回顾篇(六):js中的正则表达式 -- RegExp 对象 1. js正则表达式匹配字符之含义 查找以八进制数 规定的字符. 查找以十六进制数 规定 ...
- JavaScript RegExp对象的exec()方法
JavaScript RegExp对象的exec()方法用来匹配字符串,它的行为与match()有些不同. 对于RegExpObject.exec(),w3school上面是这样介绍的: exec() ...
- [转]JavaScript RegExp 对象参考手册
JavaScript RegExp 对象参考手册 RegExp 对象 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 直接量语法 /pattern/attributes 创建 ...
- 【timeisprecious】【JavaScript 】JavaScript RegExp 对象
JavaScript>RegExp正则表达式 1 .From Runnob JavaScript RegExp 对象(概览) JavaScript RegExp 对象(教程) RegExp 对象 ...
随机推荐
- Ring3下的DLL注入(NtCreateThreadEx + LdrLoadDll方式实现,可以注入系统进程)
工具介绍及使用请移步:http://blog.csdn.net/sunflover454/article/details/50441014 本文首发在零日安全论坛:http://www.jmpoep. ...
- tangram2.6(XE2)\framework框架加载包异常 调试的地方
添加以下的项目到项目组中, \tangram2.6(XE2)\framework\Core\Tangram_Core.dpk 调试此包的SysModuleMgr.pas的函数,本人还没有测试 func ...
- TScrollBox的用法 滚动事件
//滚轮事件:ScrollBox1: TScrollBox; procedure TfrmReleateGQAccount.ScrollBox1MouseWheel(Sender: TObject; ...
- dedecms在列表或首页取得文章首图的功能改进
在网上找过资料,效果不是很满意,第一个是原理说的不对,第二个是后缀写死. 原文大致如下: 当文章缩略图是自动选取文章内第一个图片裁减所得时 他的命名规则是有规律的 比如原文是1.jpg 它对应的缩略图 ...
- Create a REST API with Attribute Routing in ASP.NET Web API 2
原文:http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute- ...
- requirejs:模块加载(require)及定义(define)时的路径小结
原文地址:http://www.tuicool.com/articles/7JBnmy 接触过requirejs的童鞋可能都知道,无论是通过define来定义模块,还是通过require来加载模块,模 ...
- log4j日志文件 log4j.xml log4j.properties配置
1,导入log4j jar包; 2,配置log4j.xml或log4j.properties文件; ------------------------------------------------- ...
- for循环
1.使用for循环输出矩形 public static void print1(int h,int w){ for(int i=0; i<h; i++){ f ...
- c++利用循环数组建立FIFO模板队列
可直接编译运行,其中status()方法效果如图: #include <iostream> using std::cout; template<typename T> clas ...
- shape
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http:/ ...