ECMA-262 (12th Edition) ECMAScript 2021 Language Specification - page 52

 

  Главная      Manuals     ECMA-262 (12th Edition) ECMAScript 2021 Language Specification

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     50      51      52      53     ..

 

 

 

ECMA-262 (12th Edition) ECMAScript 2021 Language Specification - page 52

 

 

The following steps are taken:

1.  Let 

S

 be the 

this

 value.

2.  Return ? 

TrimString

(

S

end

).

NOTE

This function interprets a String value as a sequence of UTF-16 encoded code points, as described in 

6.1.4

.

The following steps are taken:

1.  Let 

S

 be the 

this

 value.

2.  Return ? 

TrimString

(

S

start

).

NOTE

When the 

valueOf

valueOf

 method is called, the following steps are taken:

1.  Return ? 

thisStringValue

(

this

 value).

When the 

@@iterator

@@iterator

 method is called it returns an Iterator object (

27.1.1.2

) that iterates over the code points of a

String value, returning each code point as a String value. The following steps are taken:

1.  Let 

O

 be ? 

RequireObjectCoercible

(

this

 value).

2.  Let 

s

 be ? 

ToString

(

O

).

3.  Let 

closure

 be a new 

Abstract Closure

 with no parameters that captures 

s

 and performs the following steps

when called:

a.  Let 

position

 be 0.

b.  Let 

len

 be the length of 

s

.

c.  Repeat, while 

position

 < 

len

,

i.  Let 

cp

 be ! 

CodePointAt

(

s

position

).

ii.  Let 

nextIndex

 be 

position

 + 

cp

.[[CodeUnitCount]].

iii.  Let 

resultString

 be the 

substring

 of 

s

 from 

position

 to 

nextIndex

.

iv.  Set 

position

 to 

nextIndex

.

v.  Perform ? 

Yield

(

resultString

).

d.  Return 

undefined

.

4.  Return ! 

CreateIteratorFromClosure

(

closure

"%StringIteratorPrototype%"

%StringIteratorPrototype%

).

The value of the 

"name"

 property of this function is 

"[Symbol.iterator]"

.

The 

trimEnd

trimEnd

 function is intentionally generic; it does not require that its 

this

 value be a String

object. Therefore, it can be transferred to other kinds of objects for use as a method.

The 

trimStart

trimStart

 function is intentionally generic; it does not require that its 

this

 value be a

String object. Therefore, it can be transferred to other kinds of objects for use as a method.

22.1.3.31  String.prototype.trimStart ( )

22.1.3.32  String.prototype.valueOf ( )

22.1.3.33  String.prototype [ @@iterator ] ( )

573

String instances are String exotic objects and have the internal methods specified for such objects. String instances
inherit properties from the 

String prototype object

. String instances also have a [[StringData]] internal slot.

String instances have a 

"length"

 property, and a set of enumerable properties with 

integer

-indexed names.

The number of elements in the String value represented by this String object.

Once a String object is initialized, this property is unchanging. It has the attributes { [[Writable]]: 

false

, [[Enumerable]]:

false

, [[Configurable]]: 

false

 }.

A String Iterator is an object, that represents a specific iteration over some specific String instance object. There is not a
named 

constructor

 for String Iterator objects. Instead, String iterator objects are created by calling certain methods of

String instance objects.

The 

%StringIteratorPrototype%

 object:

has properties that are inherited by all String Iterator Objects.
is an 

ordinary object

.

has a [[Prototype]] internal slot whose value is 

%IteratorPrototype%

.

has the following properties:

1.  Return ? 

GeneratorResume

(

this

 value, 

empty

"%StringIteratorPrototype%"

).

The initial value of the 

@@toStringTag

 property is the String value 

"String Iterator"

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

A RegExp object contains a regular expression and the associated flags.

NOTE

The form and functionality of regular expressions is modelled after the regular expression facility
in the Perl 5 programming language.

22.1.4  Properties of String Instances

22.1.4.1  length

22.1.5  String Iterator Objects

22.1.5.1  The %StringIteratorPrototype% Object

22.1.5.1.1  %StringIteratorPrototype%.next ( )

22.1.5.1.2  %StringIteratorPrototype% [ @@toStringTag ]

22.2  RegExp (Regular Expression) Objects

22.2.1  Patterns

574

The RegExp 

constructor

 applies the following grammar to the input pattern String. An error occurs if the grammar

cannot interpret the String as an expansion of 

Pattern

.

Pattern

[U, N]

 

::

Disjunction

[?U, ?N]

Disjunction

[U, N]

 

::

Alternative

[?U, ?N]

Alternative

[?U, ?N]

 

|

 

Disjunction

[?U, ?N]

Alternative

[U, N]

 

::

[empty]

Alternative

[?U, ?N]

 

Term

[?U, ?N]

Term

[U, N]

 

::

Assertion

[?U, ?N]

Atom

[?U, ?N]

Atom

[?U, ?N]

 

Quantifier

Assertion

[U, N]

 

::

^

$

\

 

b

\

 

B

(

 

?

 

=

 

Disjunction

[?U, ?N]

 

)

(

 

?

 

!

 

Disjunction

[?U, ?N]

 

)

(

 

?

 

<=

 

Disjunction

[?U, ?N]

 

)

(

 

?

 

<!

 

Disjunction

[?U, ?N]

 

)

Quantifier

 

::

QuantifierPrefix
QuantifierPrefix

 

?

QuantifierPrefix

 

::

*

+

?

{

 

DecimalDigits

[~Sep]

 

}

{

 

DecimalDigits

[~Sep]

 

,

 

}

{

 

DecimalDigits

[~Sep]

 

,

 

DecimalDigits

[~Sep]

 

}

Atom

[U, N]

 

::

PatternCharacter

.

Syntax

575

\

 

AtomEscape

[?U, ?N]

CharacterClass

[?U]

(

 

GroupSpecifier

[?U]

 

Disjunction

[?U, ?N]

 

)

(

 

?

 

:

 

Disjunction

[?U, ?N]

 

)

SyntaxCharacter

 

::

 

one of

^

 

$

 

\

 

.

 

*

 

+

 

?

 

(

 

)

 

[

 

]

 

{

 

}

 

|

PatternCharacter

 

::

SourceCharacter

 but not 

SyntaxCharacter

AtomEscape

[U, N]

 

::

DecimalEscape
CharacterClassEscape

[?U]

CharacterEscape

[?U]

[+N]

 

k

 

GroupName

[?U]

CharacterEscape

[U]

 

::

ControlEscape

c

 

ControlLetter

0

 [lookahead 

 

DecimalDigit

]

HexEscapeSequence
RegExpUnicodeEscapeSequence

[?U]

IdentityEscape

[?U]

ControlEscape

 

::

 

one of

f

 

n

 

r

 

t

 

v

ControlLetter

 

::

 

one of

a

 

b

 

c

 

d

 

e

 

f

 

g

 

h

 

i

 

j

 

k

 

l

 

m

 

n

 

o

 

p

 

q

 

r

 

s

 

t

 

u

 

v

 

w

 

x

 

y

 

z

 

A

 

B

 

C

 

D

 

E

 

F

 

G

 

H

 

I

 

J

 

K

 

L

 

M

 

N

 

O

 

P

 

Q

 

R

 

S

 

T

 

U

 

V

 

W

 

X

 

Y

 

Z

GroupSpecifier

[U]

 

::

[empty]

?

 

GroupName

[?U]

GroupName

[U]

 

::

<

 

RegExpIdentifierName

[?U]

 

>

RegExpIdentifierName

[U]

 

::

RegExpIdentifierStart

[?U]

RegExpIdentifierName

[?U]

 

RegExpIdentifierPart

[?U]

RegExpIdentifierStart

[U]

 

::

UnicodeIDStart

$

576

_

\

 

RegExpUnicodeEscapeSequence

[+U]

[~U]

 

UnicodeLeadSurrogate

 

UnicodeTrailSurrogate

RegExpIdentifierPart

[U]

 

::

UnicodeIDContinue

$

\

 

RegExpUnicodeEscapeSequence

[+U]

[~U]

 

UnicodeLeadSurrogate

 

UnicodeTrailSurrogate

<ZWNJ>

<ZWJ>

RegExpUnicodeEscapeSequence

[U]

 

::

[+U]

 

u

 

HexLeadSurrogate

 

\u

 

HexTrailSurrogate

[+U]

 

u

 

HexLeadSurrogate

[+U]

 

u

 

HexTrailSurrogate

[+U]

 

u

 

HexNonSurrogate

[~U]

 

u

 

Hex4Digits

[+U]

 

u{

 

CodePoint

 

}

UnicodeLeadSurrogate

 

::

any Unicode code point in the inclusive range 0xD800 to 0xDBFF

UnicodeTrailSurrogate

 

::

any Unicode code point in the inclusive range 0xDC00 to 0xDFFF

Each 

\u

\u

 

HexTrailSurrogate

 for which the choice of associated 

uu

 

HexLeadSurrogate

 is ambiguous shall be associated with

the nearest possible 

uu

 

HexLeadSurrogate

 that would otherwise have no corresponding 

\u

\u

 

HexTrailSurrogate

.

HexLeadSurrogate

 

::

Hex4Digits

 but only if the MV of 

Hex4Digits

 is in the inclusive range 0xD800 to 0xDBFF

HexTrailSurrogate

 

::

Hex4Digits

 but only if the MV of 

Hex4Digits

 is in the inclusive range 0xDC00 to 0xDFFF

HexNonSurrogate

 

::

Hex4Digits

 but only if the MV of 

Hex4Digits

 is not in the inclusive range 0xD800 to 0xDFFF

IdentityEscape

[U]

 

::

[+U]

SyntaxCharacter

[+U]

/

[~U]

SourceCharacter

 but not 

UnicodeIDContinue

DecimalEscape

 

::

NonZeroDigit

 

DecimalDigits

[~Sep]

opt

 [lookahead 

 

DecimalDigit

]

CharacterClassEscape

[U]

 

::

d

577

NonemptyClassRangesNoDash

[U]

 

::

ClassAtom

[?U]

ClassAtomNoDash

[?U]

 

NonemptyClassRangesNoDash

[?U]

ClassAtomNoDash

[?U]

 

-

 

ClassAtom

[?U]

 

ClassRanges

[?U]

ClassAtom

[U]

 

::

-

ClassAtomNoDash

[?U]

ClassAtomNoDash

[U]

 

::

SourceCharacter

 but not one of 

\

 or 

]

 or 

-

\

 

ClassEscape

[?U]

ClassEscape

[U]

 

::

b

[+U]

-

CharacterClassEscape

[?U]

CharacterEscape

[?U]

NOTE

NOTE

Pattern

 

::

 

Disjunction

It is a Syntax Error if 

NcapturingParens

 

 2

32

 - 1.

It is a Syntax Error if 

Pattern

 contains multiple 

GroupSpecifier

s whose enclosed 

RegExpIdentifierName

s have the

same 

CapturingGroupName

.

QuantifierPrefix

 

::

 

{

 

DecimalDigits

 

,

 

DecimalDigits

 

}

It is a Syntax Error if the MV of the first 

DecimalDigits

 is larger than the MV of the second 

DecimalDigits

.

AtomEscape

 

::

 

k

 

GroupName

It is a Syntax Error if the enclosing 

Pattern

 does not contain a 

GroupSpecifier

 with an enclosed 

RegExpIdentifierName

 whose 

CapturingGroupName

 equals the 

CapturingGroupName

 of the 

RegExpIdentifierName

 of this production's 

GroupName

.

AtomEscape

 

::

 

DecimalEscape

It is a Syntax Error if the 

CapturingGroupNumber

 of 

DecimalEscape

 is larger than 

NcapturingParens

 (

22.2.2.1

).

NonemptyClassRanges

 

::

 

ClassAtom

 

-

 

ClassAtom

 

ClassRanges

It is a Syntax Error if 

IsCharacterClass

 of the first 

ClassAtom

 is 

true

 or 

IsCharacterClass

 of the second 

ClassAtom

A number of productions in this section are given alternative definitions in section 

B.1.4

.

This section is amended in 

B.1.4.1

.

22.2.1.1  Static Semantics: Early Errors

579

is 

true

.

It is a Syntax Error if 

IsCharacterClass

 of the first 

ClassAtom

 is 

false

 and 

IsCharacterClass

 of the second 

ClassAtom

 is 

false

 and the 

CharacterValue

 of the first 

ClassAtom

 is larger than the 

CharacterValue

 of the second 

ClassAtom

.

NonemptyClassRangesNoDash

 

::

 

ClassAtomNoDash

 

-

 

ClassAtom

 

ClassRanges

It is a Syntax Error if 

IsCharacterClass

 of 

ClassAtomNoDash

 is 

true

 or 

IsCharacterClass

 of 

ClassAtom

 is 

true

.

It is a Syntax Error if 

IsCharacterClass

 of 

ClassAtomNoDash

 is 

false

 and 

IsCharacterClass

 of 

ClassAtom

 is 

false

and the 

CharacterValue

 of 

ClassAtomNoDash

 is larger than the 

CharacterValue

 of 

ClassAtom

.

RegExpIdentifierStart

[U]

 

::

 

\

 

RegExpUnicodeEscapeSequence

[+U]

It is a Syntax Error if the 

CharacterValue

 of 

RegExpUnicodeEscapeSequence

 is not the code point value of 

"$"

"_"

,

or some code point matched by the 

UnicodeIDStart

 lexical grammar production.

RegExpIdentifierStart

[U]

 

::

 

UnicodeLeadSurrogate

 

UnicodeTrailSurrogate

It is a Syntax Error if the result of performing 

UTF16SurrogatePairToCodePoint

 on the two code points

matched by 

UnicodeLeadSurrogate

 and 

UnicodeTrailSurrogate

 respectively is not matched by the 

UnicodeIDStart

lexical grammar production.

RegExpIdentifierPart

[U]

 

::

 

\

 

RegExpUnicodeEscapeSequence

[+U]

It is a Syntax Error if the 

CharacterValue

 of 

RegExpUnicodeEscapeSequence

 is not the code point value of 

"$"

"_"

,

<ZWNJ>, <ZWJ>, or some code point matched by the 

UnicodeIDContinue

 lexical grammar production.

RegExpIdentifierPart

[U]

 

::

 

UnicodeLeadSurrogate

 

UnicodeTrailSurrogate

It is a Syntax Error if the result of performing 

UTF16SurrogatePairToCodePoint

 on the two code points

matched by 

UnicodeLeadSurrogate

 and 

UnicodeTrailSurrogate

 respectively is not matched by the 

UnicodeIDContinue

 lexical grammar production.

UnicodePropertyValueExpression

 

::

 

UnicodePropertyName

 

=

 

UnicodePropertyValue

It is a Syntax Error if the 

List

 of Unicode code points that is 

SourceText

 of 

UnicodePropertyName

 is not identical

to a 

List

 of Unicode code points that is a Unicode 

property name

 or property alias listed in the “

Property name

and aliases” column of 

Table 56

.

It is a Syntax Error if the 

List

 of Unicode code points that is 

SourceText

 of 

UnicodePropertyValue

 is not identical

to a 

List

 of Unicode code points that is a value or value alias for the Unicode property or property alias given

by 

SourceText

 of 

UnicodePropertyName

 listed in the “Property value and aliases” column of the corresponding

tables 

Table 58

 or 

Table 59

.

UnicodePropertyValueExpression

 

::

 

LoneUnicodePropertyNameOrValue

It is a Syntax Error if the 

List

 of Unicode code points that is 

SourceText

 of 

LoneUnicodePropertyNameOrValue

 is

not identical to a 

List

 of Unicode code points that is a Unicode general category or general category alias listed

in the “Property value and aliases” column of 

Table 58

, nor a binary property or binary property alias listed in

the “

Property name

 and aliases” column of 

Table 57

.

22.2.1.2  Static Semantics: CapturingGroupNumber

580

NOTE

DecimalEscape

 

::

 

NonZeroDigit

1.  Return the MV of 

NonZeroDigit

.

DecimalEscape

 

::

 

NonZeroDigit

 

DecimalDigits

1.  Let 

n

 be the number of code points in 

DecimalDigits

.

2.  Return (the MV of 

NonZeroDigit

 

×

 10

n

 plus the MV of 

DecimalDigits

).

The definitions of “the MV of 

NonZeroDigit

” and “the MV of 

DecimalDigits

” are in 

12.8.3

.

NOTE

ClassAtom

 

::

 

-

ClassAtomNoDash

 

::

 

SourceCharacter

 but not one of 

\

 or 

]

 or 

-

ClassEscape

 

::

 

b

ClassEscape

 

::

 

-

ClassEscape

 

::

 

CharacterEscape

1.  Return 

false

.

ClassEscape

 

::

 

CharacterClassEscape

1.  Return 

true

.

NOTE 1

ClassAtom

 

::

 

-

1.  Return the code point value of U+002D (HYPHEN-MINUS).

ClassAtomNoDash

 

::

 

SourceCharacter

 but not one of 

\

 or 

]

 or 

-

1.  Let 

ch

 be the code point matched by 

SourceCharacter

.

2.  Return the code point value of 

ch

.

ClassEscape

 

::

 

b

1.  Return the code point value of U+0008 (BACKSPACE).

ClassEscape

 

::

 

-

1.  Return the code point value of U+002D (HYPHEN-MINUS).

CharacterEscape

 

::

 

ControlEscape

This section is amended in 

B.1.4.1

.

This section is amended in 

B.1.4.2

.

This section is amended in 

B.1.4.3

.

22.2.1.3  Static Semantics: IsCharacterClass

22.2.1.4  Static Semantics: CharacterValue

581

1.  Return the code point value according to 

Table 55

.

Table 55: ControlEscape Code Point Values

ControlEscape Code Point Value Code Point

Unicode Name

Symbol

tt

9

U+0009

U+0009

CHARACTER TABULATION <HT>

nn

10

U+000A

U+000A

LINE FEED (LF)

<LF>

vv

11

U+000B

U+000B

LINE TABULATION

<VT>

ff

12

U+000C

U+000C

FORM FEED (FF)

<FF>

rr

13

U+000D

U+000D

CARRIAGE RETURN (CR)

<CR>

CharacterEscape

 

::

 

c

 

ControlLetter

1.  Let 

ch

 be the code point matched by 

ControlLetter

.

2.  Let 

i

 be 

ch

's code point value.

3.  Return the remainder of dividing 

i

 by 32.

CharacterEscape

 

::

 

0

 [lookahead 

 

DecimalDigit

]

1.  Return the code point value of U+0000 (NULL).

NOTE 2

CharacterEscape

 

::

 

HexEscapeSequence

1.  Return the MV of 

HexEscapeSequence

.

RegExpUnicodeEscapeSequence

 

::

 

u

 

HexLeadSurrogate

 

\u

 

HexTrailSurrogate

1.  Let 

lead

 be the 

CharacterValue

 of 

HexLeadSurrogate

.

2.  Let 

trail

 be the 

CharacterValue

 of 

HexTrailSurrogate

.

3.  Let 

cp

 be 

UTF16SurrogatePairToCodePoint

(

lead

trail

).

4.  Return the code point value of 

cp

.

RegExpUnicodeEscapeSequence

 

::

 

u

 

Hex4Digits

1.  Return the MV of 

Hex4Digits

.

RegExpUnicodeEscapeSequence

 

::

 

u{

 

CodePoint

 

}

1.  Return the MV of 

CodePoint

.

HexLeadSurrogate

 

::

 

Hex4Digits

HexTrailSurrogate

 

::

 

Hex4Digits

HexNonSurrogate

 

::

 

Hex4Digits

1.  Return the MV of 

HexDigits

.

\0

\0

 represents the <NUL> character and cannot be followed by a decimal digit.

582

CharacterEscape

 

::

 

IdentityEscape

1.  Let 

ch

 be the code point matched by 

IdentityEscape

.

2.  Return the code point value of 

ch

.

UnicodePropertyNameCharacters

 

::

 

UnicodePropertyNameCharacter

 

UnicodePropertyNameCharacters

opt

UnicodePropertyValueCharacters

 

::

 

UnicodePropertyValueCharacter

 

UnicodePropertyValueCharacters

opt

1.  Return the 

List

, in source text order, of Unicode code points in the source text matched by this production.

RegExpIdentifierName

[U]

 

::

RegExpIdentifierStart

[?U]

RegExpIdentifierName

[?U]

 

RegExpIdentifierPart

[?U]

1.  Let 

idText

 be the source text matched by 

RegExpIdentifierName

.

2.  Let 

idTextUnescaped

 be the result of replacing any occurrences of 

\\

 

RegExpUnicodeEscapeSequence

 in 

idText

 with

the code point represented by the 

RegExpUnicodeEscapeSequence

.

3.  Return ! 

CodePointsToString

(

idTextUnescaped

).

NOTE 1

A regular expression pattern is converted into an 

Abstract Closure

 using the process described below. An

implementation is encouraged to use more efficient algorithms than the ones listed below, as long as the results are the
same. The 

Abstract Closure

 is used as the value of a RegExp object's [[RegExpMatcher]] internal slot.

Pattern

 is either a BMP pattern or a Unicode pattern depending upon whether or not its associated flags contain a 

uu

.

A BMP pattern matches against a String interpreted as consisting of a sequence of 16-bit values that are Unicode code
points in the range of the Basic Multilingual Plane. A Unicode pattern matches against a String interpreted as
consisting of Unicode code points encoded using UTF-16. In the context of describing the behaviour of a BMP pattern
“character” means a single 16-bit Unicode BMP code point. In the context of describing the behaviour of a Unicode
pattern “character” means a UTF-16 encoded code point (

6.1.4

). In either context, “character value” means the

numeric value of the corresponding non-encoded code point.

The syntax and semantics of 

Pattern

 is defined as if the source code for the 

Pattern

 was a 

List

 of 

SourceCharacter

 values

where each 

SourceCharacter

 corresponds to a Unicode code point. If a BMP pattern contains a non-BMP 

SourceCharacter

the entire pattern is encoded using UTF-16 and the individual code units of that encoding are used as the elements of
the 

List

.

This section is amended in 

B.1.4.4

.

22.2.1.5  Static Semantics: SourceText

22.2.1.6  Static Semantics: CapturingGroupName

22.2.2  Pattern Semantics

583

NOTE 2

The descriptions below use the following aliases:

Input

 is a 

List

 whose elements are the characters of the String being matched by the regular expression pattern.

Each character is either a code unit or a code point, depending upon the kind of pattern involved. The notation

Input

[

n

] means the 

n

th

 character of 

Input

, where 

n

 can range between 0 (inclusive) and 

InputLength

 (exclusive).

InputLength

 is the number of characters in 

Input

.

NcapturingParens

 is the total number of left-capturing parentheses (i.e. the total number of 

Atom

 

::

 

(

GroupSpecifier

 

Disjunction

 

)

 Parse Nodes) in the pattern. A left-capturing parenthesis is any 

((

 pattern

character that is matched by the 

((

 terminal of the 

Atom

 

::

 

(

 

GroupSpecifier

 

Disjunction

 

)

 production.

DotAll

 is 

true

 if the RegExp object's [[OriginalFlags]] internal slot contains 

"s"

 and otherwise is 

false

.

IgnoreCase

 is 

true

 if the RegExp object's [[OriginalFlags]] internal slot contains 

"i"

 and otherwise is 

false

.

Multiline

 is 

true

 if the RegExp object's [[OriginalFlags]] internal slot contains 

"m"

 and otherwise is 

false

.

Unicode

 is 

true

 if the RegExp object's [[OriginalFlags]] internal slot contains 

"u"

 and otherwise is 

false

.

 

WordCharacters

 is the mathematical set that is the union of all sixty-three characters in

"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"

 (letters, numbers, and

U+005F (LOW LINE) in the Unicode Basic Latin block) and all characters 

c

 for which 

c

 is not in that set but

Canonicalize

(

c

) is. 

WordCharacters

 cannot contain more than sixty-three characters unless 

Unicode

 and

IgnoreCase

 are both 

true

.

Furthermore, the descriptions below use the following internal data structures:

CharSet

 is a mathematical set of characters. When the 

Unicode

 flag is 

true

, “all characters” means the CharSet

containing all code point values; otherwise “all characters” means the CharSet containing all code unit values.

State

 is an ordered pair (

endIndex

captures

) where 

endIndex

 is an 

integer

 and 

captures

 is a 

List

 of

NcapturingParens

 values. States are used to represent partial match states in the regular expression matching

algorithms. The 

endIndex

 is one plus the index of the last input character matched so far by the pattern, while

captures

 holds the results of capturing parentheses. The 

n

th

 element of 

captures

 is either a 

List

 of characters that

represents the value obtained by the 

n

th

 set of capturing parentheses or 

undefined

 if the 

n

th

 set of capturing

For example, consider a pattern expressed in source text as the single non-BMP character
U+1D11E (MUSICAL SYMBOL G CLEF). Interpreted as a Unicode pattern, it would be a single
element (character) 

List

 consisting of the single code point 0x1D11E. However, interpreted as a

BMP pattern, it is first UTF-16 encoded to produce a two element 

List

 consisting of the code units

0xD834 and 0xDD1E.

Patterns are passed to the RegExp 

constructor

 as ECMAScript String values in which non-BMP

characters are UTF-16 encoded. For example, the single character MUSICAL SYMBOL G CLEF
pattern, expressed as a String value, is a String of length 2 whose elements were the code units
0xD834 and 0xDD1E. So no further translation of the string would be necessary to process it as a
BMP pattern consisting of two pattern characters. However, to process it as a Unicode pattern

UTF16SurrogatePairToCodePoint

 must be used in producing a 

List

 whose sole element is a single

pattern character, the code point U+1D11E.

An implementation may not actually perform such translations to or from UTF-16, but the
semantics of this specification requires that the result of pattern matching be as if such
translations were performed.

22.2.2.1  Notation

584

parentheses hasn't been reached yet. Due to backtracking, many States may be in use at any time during the
matching process.

MatchResult

 is either a State or the special token 

failure

 that indicates that the match failed.

Continuation

 is an 

Abstract Closure

 that takes one State argument and returns a MatchResult result. The

Continuation attempts to match the remaining portion (specified by the closure's captured values) of the
pattern against 

Input

, starting at the intermediate state given by its State argument. If the match succeeds, the

Continuation returns the final State that it reached; if the match fails, the Continuation returns 

failure

.

Matcher

 is an 

Abstract Closure

 that takes two arguments—a State and a Continuation—and returns a

MatchResult result. A Matcher attempts to match a middle subpattern (specified by the closure's captured
values) of the pattern against 

Input

, starting at the intermediate state given by its State argument. The

Continuation argument should be a closure that matches the rest of the pattern. After matching the subpattern
of a pattern to obtain a new State, the Matcher then calls Continuation on that new State to test if the rest of the
pattern can match as well. If it can, the Matcher returns the State returned by Continuation; if not, the Matcher
may try different choices at its choice points, repeatedly calling Continuation until it either succeeds or all
possibilities have been exhausted.

The production 

Pattern

 

::

 

Disjunction

 evaluates as follows:

1.  Evaluate 

Disjunction

 with 1 as its 

direction

 argument to obtain a Matcher 

m

.

2.  Return a new 

Abstract Closure

 with parameters (

str

index

) that captures 

m

 and performs the following steps

when called:

a. 

Assert

Type

(

str

) is String.

b. 

Assert

index

 is a non-negative 

integer

 which is 

 the length of 

str

.

c.  If 

Unicode

 is 

true

, let 

Input

 be ! 

StringToCodePoints

(

str

). Otherwise, let 

Input

 be a 

List

 whose elements

are the code units that are the elements of 

str

Input

 will be used throughout the algorithms in 

22.2.2

.

Each element of 

Input

 is considered to be a character.

d.  Let 

InputLength

 be the number of characters contained in 

Input

. This alias will be used throughout the

algorithms in 

22.2.2

.

e.  Let 

listIndex

 be the index into 

Input

 of the character that was obtained from element 

index

 of 

str

.

f.  Let 

c

 be a new Continuation with parameters (

y

) that captures nothing and performs the following steps

when called:

i. 

Assert

y

 is a State.

ii.  Return 

y

.

g.  Let 

cap

 be a 

List

 of 

NcapturingParens

 

undefined

 values, indexed 1 through 

NcapturingParens

.

h.  Let 

x

 be the State (

listIndex

cap

).

i.  Return 

m

(

x

c

).

NOTE

A Pattern evaluates (“compiles”) to an 

Abstract Closure

 value. 

RegExpBuiltinExec

 can then apply

this procedure 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 

22.2.2

 are designed so that compiling a

pattern may throw a 

SyntaxError

 exception; on the other hand, once the pattern is successfully

compiled, applying the resulting 

Abstract Closure

 to find a match in a String cannot throw an

exception (except for any 

implementation-defined

 exceptions that can occur anywhere such as

out-of-memory).

22.2.2.2  Pattern

585

With parameter 

direction

.

The production 

Disjunction

 

::

 

Alternative

 evaluates as follows:

1.  Evaluate 

Alternative

 with argument 

direction

 to obtain a Matcher 

m

.

2.  Return 

m

.

The production 

Disjunction

 

::

 

Alternative

 

|

 

Disjunction

 evaluates as follows:

1.  Evaluate 

Alternative

 with argument 

direction

 to obtain a Matcher 

m1

.

2.  Evaluate 

Disjunction

 with argument 

direction

 to obtain a Matcher 

m2

.

3.  Return a new Matcher with parameters (

x

c

) that captures 

m1

 and 

m2

 and performs the following steps when

called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Let 

r

 be 

m1

(

x

c

).

d.  If 

r

 is not 

failure

, return 

r

.

e.  Return 

m2

(

x

c

).

NOTE

With parameter 

direction

.

The production 

Alternative

 

::

  [empty]  evaluates as follows:

1.  Return a new Matcher with parameters (

x

c

) that captures nothing and performs the following steps when

The 

||

 regular expression operator separates two alternatives. The pattern first tries to match the

left 

Alternative

 (followed by the sequel of the regular expression); if it fails, it tries to match the

right 

Disjunction

 (followed by the sequel of the regular expression). If the left 

Alternative

, the right

Disjunction

, and the sequel all have choice points, all choices in the sequel are tried before moving

on to the next choice in the left 

Alternative

. If choices in the left 

Alternative

 are exhausted, the right

Disjunction

 is tried instead of the left 

Alternative

. Any capturing parentheses inside a portion of

the pattern skipped by 

||

 produce 

undefined

 values instead of Strings. Thus, for example,

returns the result 

"a"

 and not 

"ab"

. Moreover,

returns the array

and not

The order in which the two alternatives are tried is independent of the value of 

direction

.

/a|ab/.exec(

"abc"

)

/((a)|(ab))((c)|(bc))/.exec(

"abc"

)

[

"abc"

"a"

"a"

undefined

"bc"

undefined

"bc"

]

[

"abc"

"ab"

undefined

"ab"

"c"

"c"

undefined

]

22.2.2.3  Disjunction

22.2.2.4  Alternative

586

called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Return 

c

(

x

).

The production 

Alternative

 

::

 

Alternative

 

Term

 evaluates as follows:

1.  Evaluate 

Alternative

 with argument 

direction

 to obtain a Matcher 

m1

.

2.  Evaluate 

Term

 with argument 

direction

 to obtain a Matcher 

m2

.

3.  If 

direction

 = 1, then

a.  Return a new Matcher with parameters (

x

c

) that captures 

m1

 and 

m2

 and performs the following steps

when called:

i. 

Assert

x

 is a State.

ii. 

Assert

c

 is a Continuation.

iii.  Let 

d

 be a new Continuation with parameters (

y

) that captures 

c

 and 

m2

 and performs the

following steps when called:

1. 

Assert

y

 is a State.

2.  Return 

m2

(

y

c

).

iv.  Return 

m1

(

x

d

).

4.  Else,

a. 

Assert

direction

 is -1.

b.  Return a new Matcher with parameters (

x

c

) that captures 

m1

 and 

m2

 and performs the following steps

when called:

i. 

Assert

x

 is a State.

ii. 

Assert

c

 is a Continuation.

iii.  Let 

d

 be a new Continuation with parameters (

y

) that captures 

c

 and 

m1

 and performs the

following steps when called:

1. 

Assert

y

 is a State.

2.  Return 

m1

(

y

c

).

iv.  Return 

m2

(

x

d

).

NOTE

With parameter 

direction

.

The production 

Term

 

::

 

Assertion

 evaluates as follows:

1.  Return the Matcher that is the result of evaluating 

Assertion

.

NOTE

The production 

Term

 

::

 

Atom

 evaluates as follows:

Consecutive 

Term

s try to simultaneously match consecutive portions of 

Input

. When 

direction

 = 1,

if the left 

Alternative

, the right 

Term

, and the sequel of the regular expression all have choice

points, all choices in the sequel are tried before moving on to the next choice in the right 

Term

,

and all choices in the right 

Term

 are tried before moving on to the next choice in the left 

Alternative

. When 

direction

 = -1, the evaluation order of 

Alternative

 and 

Term

 are reversed.

The resulting Matcher is independent of 

direction

.

22.2.2.5  Term

587

1.  Return the Matcher that is the result of evaluating 

Atom

 with argument 

direction

.

The production 

Term

 

::

 

Atom

 

Quantifier

 evaluates as follows:

1.  Evaluate 

Atom

 with argument 

direction

 to obtain a Matcher 

m

.

2.  Evaluate 

Quantifier

 to obtain the three results: a non-negative 

integer

 

min

, a non-negative 

integer

 (or +

max

,

and Boolean 

greedy

.

3. 

Assert

min

 

 

max

.

4.  Let 

parenIndex

 be the number of left-capturing parentheses in the entire regular expression that occur to the left

of this 

Term

. This is the total number of 

Atom

 

::

 

(

 

GroupSpecifier

 

Disjunction

 

)

 Parse Nodes prior to or

enclosing this 

Term

.

5.  Let 

parenCount

 be the number of left-capturing parentheses in 

Atom

. This is the total number of 

Atom

 

::

 

(

GroupSpecifier

 

Disjunction

 

)

 Parse Nodes enclosed by 

Atom

.

6.  Return a new Matcher with parameters (

x

c

) that captures 

m

min

max

greedy

parenIndex

, and 

parenCount

 and

performs the following steps when called:

a. 

Assert

x

 is a State.

b. 

Assert

c

 is a Continuation.

c.  Return ! 

RepeatMatcher

(

m

min

max

greedy

x

c

parenIndex

parenCount

).

The abstract operation RepeatMatcher takes arguments 

m

 (a Matcher), 

min

 (a non-negative 

integer

), 

max

 (a non-

negative 

integer

 or +

), 

greedy

 (a Boolean), 

x

 (a State), 

c

 (a Continuation), 

parenIndex

 (a non-negative 

integer

), and

parenCount

 (a non-negative 

integer

). It performs the following steps when called:

1.  If 

max

 = 0, return 

c

(

x

).

2.  Let 

d

 be a new Continuation with parameters (

y

) that captures 

m

min

max

greedy

x

c

parenIndex

, and

parenCount

 and performs the following steps when called:

a. 

Assert

y

 is a State.

b.  If 

min

 = 0 and 

y

's 

endIndex

 = 

x

's 

endIndex

, return 

failure

.

c.  If 

min

 = 0, let 

min2

 be 0; otherwise let 

min2

 be 

min

 - 1.

d.  If 

max

 is +

, let 

max2

 be +

; otherwise let 

max2

 be 

max

 - 1.

e.  Return ! 

RepeatMatcher

(

m

min2

max2

greedy

y

c

parenIndex

parenCount

).

3.  Let 

cap

 be a copy of 

x

's 

captures

 

List

.

4.  For each 

integer

 

k

 such that 

parenIndex

 < 

k

 and 

k

 

 

parenIndex

 + 

parenCount

, set 

cap

[

k

] to 

undefined

.

5.  Let 

e

 be 

x

's 

endIndex

.

6.  Let 

xr

 be the State (

e

cap

).

7.  If 

min

 

 0, return 

m

(

xr

d

).

8.  If 

greedy

 is 

false

, then

a.  Let 

z

 be 

c

(

x

).

b.  If 

z

 is not 

failure

, return 

z

.

c.  Return 

m

(

xr

d

).

9.  Let 

z

 be 

m

(

xr

d

).

10.  If 

z

 is not 

failure

, return 

z

.

11.  Return 

c

(

x

).

22.2.2.5.1  RepeatMatcher ( 

m

min

max

greedy

x

c

parenIndex

parenCount

 )

588

 

 

 

 

 

 

 

Content      ..     50      51      52      53     ..