Skip to content Skip to sidebar Skip to footer

Global Matches Using Javascript Regular Expressions

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 second from the selector (braces),

Solution 1:

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:

functionmatchAll(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:0input:"Foo Bar"length:3__proto__: Array[0]
, 
Array[3]
0: "Bar"1: "B"2: "ar"index:4input:"Foo Bar"length:3__proto__: Array[0]
]

Solution 2:

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.

Post a Comment for "Global Matches Using Javascript Regular Expressions"