Global Matches using JavaScript Regular Expressions

前端 未结 2 1919
你的背包
你的背包 2021-01-27 20:55

Usually when you do something like \'test\'.match(/(e)/) you would receive an array [\'e\', \'e\'], where the first element is the match itself and the

相关标签:
2条回答
  • 2021-01-27 21:37

    If the global flag (g) is not set, Element zero of the array contains the entire match, while elements 1 through n contain any submatches. This behavior is the same as the behavior of the exec Method (Regular Expression) (JavaScript) when the global flag is not set. If the global flag is set, elements 0 through n contain all matches that occurred.

    http://msdn.microsoft.com/en-us/library/ie/7df7sf4x(v=vs.94).aspx

    In other words, when g is provided, match collects only topmost matches, ignoring any capturing groups.

    Example:

    > s = "Foo Bar"
    "Foo Bar"
    > s.match(/([A-Z])([a-z]+)/)
    ["Foo", "F", "oo"]
    > s.match(/([A-Z])([a-z]+)/g)
    ["Foo", "Bar"]
    

    There's no built-in that would collect all groups from all matches, like python findall does, but it's easy to write using exec:

    function matchAll(re, str) {
        var p, r = [];
        while(p = re.exec(str))
            r.push(p);
        return r;
    }
    matchAll(/([A-Z])([a-z]+)/g, "Foo Bar")
    

    Result:

    [
    Array[3]
    0: "Foo"
    1: "F"
    2: "oo"
    index: 0
    input: "Foo Bar"
    length: 3
    __proto__: Array[0]
    , 
    Array[3]
    0: "Bar"
    1: "B"
    2: "ar"
    index: 4
    input: "Foo Bar"
    length: 3
    __proto__: Array[0]
    ]
    
    0 讨论(0)
  • 2021-01-27 21:45

    The behavior is specified at ECMA script language spec. This section describes in detail the procedure followed by the regex engine with and without the global modifier.

    Specific at point 11: If global is true, Call the [[Put]] internal method of R with arguments "lastIndex", e, and true.

    0 讨论(0)
提交回复
热议问题