zoukankan      html  css  js  c++  java
  • RegExp.exec

    来自: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>
  • 相关阅读:
    今天遇到的报错Babel noteThe code generator has deoptimised the styling of ...as it exceeds the max of 500KB.
    轻松理解Promise.all 、Promise.then、Promise.race有什么区别以及使用方法
    JS循环类数组对象,获得页面所有的某个ClssaName,处理它的属性
    Angular在用户登录后设置授权请求头headers.append('Authorization', 'token');
    webpack配置less以及js中引入的图片问题
    超级容易理解的Three.js中的物体rotation
    新下载了一个框架,然后npm install时候报错npm ERR! Maximum call stack size exceeded
    webpack打包后发现有一部分代码还携带注释,如何解决?/webpack打包删除注释以及console.log--快快点进来看一看吧~~
    composer 下载扩展包
    postman 模拟登录状态
  • 原文地址:https://www.cnblogs.com/ghgyj/p/3315946.html
Copyright © 2011-2022 走看看