来自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec

Summary

Executes a search for a match in a specified string. Returns a result array, or null.

(在指定的字符串中寻找匹配。返回结果数组,或者null。)

Method of RegExp
Implemented in JavaScript 1.2
ECMAScript Edition ECMAScript 3rd Edition

Syntax

result = regexp.exec(str)

Parameters

regexp
The name of the regular expression. It can be a variable name or a literal.(正则表达式的名字。可以是变量的名字或者是字面量)
str
The string against which to match the regular expression.(对这个字符串正则表达式匹配)

Return value

If the match succeeds, the exec method returns an array and updates properties of the regular expression object.(如果匹配成功,exec方法返回数组并更新正则表达式对象的属性。) The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.

If the match fails, the exec method returns null.

If you are executing a match simply to find true or false, use the RegExp test method or the String search method.

Consider the following example:

<script>
// Match one d followed by one or more b's followed by one d
// Remember matched b's and the following d
// Ignore case
var re = /d(b+)(d)/ig;
var result = re.exec("cdbBdbsbz");
console.log(result[0]);//dbBd
console.log(result[1]);//bB
console.log(result[2]);//d
console.log(result[3]);//undefined
console.log(result.index);//1
console.log(result.input);//cdbBdbsbz
console.log(re.lastIndex);//5
console.log(re.ignoreCase);//true
console.log(re.global);//true
console.log(re.multiline);//false
console.log(re.source);//d(b+)(d)
</script>

The following table shows the results for this script:

Object Property/Index Description Example
result [0] The last matched characters dbBd
[1], ...[n] The parenthesized substring matches, if any. The number of possible parenthesized substrings is unlimited. [1] = bB
[2] = d
index The 0-based index of the match in the string. 1
input The original string. cdbBdbsbz
re lastIndex The index at which to start the next match. 5
ignoreCase Indicates if the "i" flag was used to ignore case. true
global Indicates if the "g" flag was used for a global match. true
multiline Indicates if the "m" flag was used to search in strings across multiple line. false
source The text of the pattern. d(b+)(d)

Notes

Finding successive matches

If your regular expression uses the "g" flag, you can use the exec method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test will also advance the lastIndex property). For example, assume you have this script:

<script>
var myRe = /ab*/g;
var str = "abbcdefabh";
var myArray;
while ((myArray = myRe.exec(str)) !== null)
{
var msg = "Found " + myArray[0] + ". ";
msg += "Next match starts at " + myRe.lastIndex;
console.log(msg);
}
</script>

This script displays the following text:

Found abb. Next match starts at 3
Found ab. Next match starts at 9

Note: Do not place the regular expression literal (or RegExp constructor) within the while condition or it will create an infinite loop if there is a match due to the lastIndex property being reset upon each iteration.

Using exec() with RegExp literals

You can also use exec() without creating a RegExp object:

<script>
var matches = /(hello \S+)/.exec('This is a hello world!');
alert(matches[1]);
</script>

This will display an alert containing 'hello world!';

Calling exec() with no parameters in old Gecko versions

Gecko 8.0 note

(Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5)

Prior to Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5), exec() was implemented incorrectly; when it was called with no parameters, it would match against the value of the previous input (RegExp.input property) instead of against the string "undefined". This is fixed; now /undefined/.exec() correctly results in ['undefined'], instead of an error.

<script>
var matches = /undefined/.exec("");
alert(matches);
matches = /undefined/.exec();
alert(matches[1]);//undefined
matches = /undefined/.exec("undefined");
alert(matches[1]);//undefined
</script>

RegExp.exec的更多相关文章

  1. js 正则表达式的使用(标志 RegExp exec() test() compile() $1...$9)

    一,标志 g (global,全局匹配标志) 执行正则表达式匹配或替换时,一般只要搜索到一个符合的文本就停止匹配或替换.使用该标志将搜索所有符合的文本直到文本末尾. i (ignoreCase,忽略大 ...

  2. string.match(RegExp) 与 RegExp.exec(string) 深入详解

    string.match(RegExp) 与 RegExp.exec(string) 相同点与不同点对比解析: 1. 这两个方法,如果匹配成功,返回一个数组,匹配失败,返回null. 2. 当RegE ...

  3. RegExp.exec和String.match深入理解

    今天在重新阅读<JavaScript权威指南>的RegExp和String的时候,看到了2个比较容易混淆的函数:RegExp的exec和String的match 这2个函数都是从指定的字符 ...

  4. RegExp exec有记忆性的问题

    当 RegExpObject 是作为一个变量时时.每次调用完exec()后.它会在 RegExpObject 的 lastIndex 属性指定的字符处开始检索字符串 string.当 exec() 找 ...

  5. JavaScript RegExp.exec() 方法

    定义和用法: exec() 方法用于检索字符串中的正则表达式的匹配. 语法: RegExpObject.exec(string); RegExpObject:必须参数,正则表达式: string:必须 ...

  6. JavaScript RegExp对象的exec()方法

    JavaScript RegExp对象的exec()方法用来匹配字符串,它的行为与match()有些不同. 对于RegExpObject.exec(),w3school上面是这样介绍的: exec() ...

  7. 原生JS:RegExp对象详解

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. 正则表达式中的exec和match方法的区别

    正则表达式中的exec和match方法的区别 字符串的正则方法有:match().replace().search().split() 正则对象的方法有:exec().test() 1.match m ...

  9. 深度解析正则表达式exec和match两者使用的异同以及要注意的地方

    1.match match方法属于String正则表达方法. 语法: str.match(regexp) str:要进行匹配的字符串. regexp:一个正则表达式(或者由RegExp()构造成的正则 ...

随机推荐

  1. [数论]ZOJ3593 One Person Game

    题意:一个人要从A走到B  只能走a布.b步.(a+b)步,可以往左或右走   问 最少要走几步,不能走到的输出-1 可以列出方程 $ax+by=A-B$ 或者 $ax+(a+b)y=A-B$ 或者 ...

  2. C#基础精华05(正则表达式,)

    正则表达式 . 任意一个字符 除了\n以外的 []  [0-9]       [0-9a-zA-Z] |  或   [0-9]|[a-z] ()   提升优先级别   分组 ([a]|[0-9])[0 ...

  3. Apache James搭建内网邮件服务器

    Apache James搭建内网邮件服务器 极客521 | 极客521 2014-08-21 148 阅读 java 大概之前两个礼拜的日子,讨论会介绍了关于了.net内网邮件服务器的搭建.所以自己也 ...

  4. linux文件系统-基本磁盘2

    直入主题-基本磁盘 硬盘数据按照不同特点和作用大致分为5部分:MBR区.DBR区.FAT区.DIR区和DATA区 1.MBR MBR(Main Boot Record 主引导记录区)位于整个硬盘的0磁 ...

  5. HTTP认证方式

    HTTP请求报头: Authorization HTTP响应报头: WWW-Authenticate   HTTP认证 基于 质询 /回应( challenge/response)的认证模式.   ◆ ...

  6. Use powerful plugins in your vim.

    # setup by root wget http://prdownloads.sourceforge.net/ctags/ctags-5.8.tar.gz tar -xzvf ctags-5.8.t ...

  7. VMware Workstation与Hyper-V不兼容。请先从系统中移除Hyper-V角色,然后再运行VMware Workstation。

    VMware Workstation与Hyper-V不兼容.请先从系统中移除Hyper-V角色,然后再运行VMware Workstation. 今天在用win8.1的时候发现了这个问题,解决办法如下 ...

  8. poj2400Supervisor, Supervisee(KM)

    http://poj.org/problem?id=2400 KM算法http://philoscience.iteye.com/blog/1754498 题意:每个雇主对雇员有个满意度 雇员对雇主有 ...

  9. 【 D3.js 选择集与数据详解 — 5 】 处理模板的应用

    在[选择集与数据 - 4]一文中,介绍了一个update.enter.exit的处理模板,这个模板很常用,本文将通过一个例子来讲解其使用方法. 1. 模板 复习一下上一章提到的模板. //绑定数据后, ...

  10. ARM Linux 如何--注册和触发--软中断

    1. 注册软中断当然是通过open_softirq 例子如下: void __init init_timers(void) { int err = timer_cpu_notify(&time ...