2.1.127 [ECMA-262-1999] Section 15.10.2.2, Pattern

V0187:

The production Pattern :: Disjunction evaluates as follows:

1. Evaluate Disjunction to obtain a Matcher m.

2. Return an internal closure that takes two arguments, a string str and an integer index, and performs the following:

  1. Let Input be the given string str. This variable will be used throughout the functions in [ECMA-262-1999] section 15.10.2.

  2. Let InputLength be the length of Input. This variable will be used throughout the functions in [ECMA-262-1999] section 15.10.2.

  3. Let c be a Continuation that always returns its State argument as a successful MatchResult.

  4. Let cap be an internal array of NCapturingParens #undefined# __empty string__ values, indexed 1 through NCapturingParens.

  5. Let x be the State (index, cap).

  6. Call m(x, c) and return its result.

Informative comments: A Pattern evaluates ("compiles") to an internal function value. RegExp.prototype.exec can then apply this function to a string and an offset within the string to determine whether the pattern would match starting at exactly that offset within the string, and, if it does match, what the values of the capturing parentheses would be. The algorithms in [ECMA-262-1999] section 15.10.2 are designed so that compiling a pattern may throw a #SyntaxError# __RegExpError__ exception; on the other hand, once the pattern is successfully compiled, applying its result function to find a match in a string cannot throw an exception (except for any host-defined exceptions that can occur anywhere such as out-of-memory).