2.1.139 [ECMA-262-1999] Section 15.10.6.2, RegExp.prototype.exec (string)

V0209:

Performs a regular expression match of string against the regular expression and returns an Array object containing the results of the match, or null if the string did not match.

The string ToString(string) is searched for an occurrence of the regular expression pattern as follows:

  1. Let S be the value of ToString(string).

  2. Let length be the length of S.

  3. Let lastIndex be the value of the lastIndex property.

  4. Let i be the value of ToInteger(lastIndex). __However if an exception is thrown while evaluating ToInteger, let I = 0, or if Result(1) of the ToInteger algorithm is NaN, let I = -1.__

  5. If the global property is false, let i = 0.

  6. If #I# __i__ < 0 or #I# __i__ > length then set lastIndex to 0 and return null.

  7. Call [[Match]], giving it the arguments S and i. If [[Match]] returned failure, go to step 8; otherwise let r be its State result and go to step 10.

  8. Let i = i+1.

  9. Go to step 6.

  10. Let e be r's endIndex value.

  11. #If the global property is true, set# __Set__ lastIndex to e.

  12. Let n be the length of r's captures array. (This is the same value as [ECMA-262-1999] section 15.10.2.1's NCapturingParens.)

    1. __The values of the RegExp.input and RegExp.$_ properties are set to S.__

    2. __The value of the RegExp.index property is set to the position of the matched substring within the complete string S.__

    3. __The value of the RegExp.lastIndex property is set to e.__

    4. __The values of the RegExp.input and RegExp.$_ properties are set to S.__

    5. __The values of the RegExp.lastMatch and RegExp['$&'] properties are set to the substring of S that was matched.__

    6. __If n is 0, set the values of the RegExp.lastParen and RegExp['$+'] properties are set to the empty string, otherwise set them to the result of calling ToString on the last element of r's captures array.__

    7. __The values of the RegExp.leftContext and RegExp["$`"] properties are set to the substring of S, starting at character position 0 and continuing up to but not including the position of the matched substring within the complete string S.__

    8. __The values of the RegExp.rightContext and RegExp["$'"] properties are set to the substring of S, starting at character position e and continuing to the last character of S.__

    9. __The value of each of the properties RegExp.$1, RegExp.$2, RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6, RegExp.$7, RegExp.$8, and RegExp.$9 is set to the empty string.__

    10. __For each integer i such that i > 0 and i ≤ min(9,n), set the property of RegExp that has the name of the string '$' concatenated with ToString(i) to the ith element of r's captures array.__

  13. Return a new array with the following properties:

    1. The index property is set to the position of the matched substring within the complete string S.

    2. The input property is set to S.

    3. __The lastIndex property is set to e.__

    4. The length property is set to n + 1.

    5. The 0 property is set to the matched substring (i.e. the portion of S between offset i inclusive and offset e exclusive).

    6. For each integer i such that #I# __i__ > 0 and #I# __i__ n, set the property named ToString(i) to the ith element of r's captures array.