22 文本处理

22.1 String 对象

22.1.1 String 构造器

String 构造器:

  • %String%
  • 全局对象"String" 属性的初始值。
  • 在作为构造器调用时创建并初始化一个新的 String 对象。
  • 在作为函数而非构造器调用时执行类型转换。
  • 可以用作类定义的 extends 子句的值。意图继承指定 String 行为的子类构造器必须包含对 String 构造器的 super 调用,以创建并初始化具有 [[StringData]] 内部槽的子类实例。

22.1.1.1 String ( value )

此函数在被调用时执行以下步骤:

  1. 如果 value 不存在,则
    1. string 为空 String。
  2. 否则,
    1. 如果 NewTarget 是 undefinedvalue 是 Symbol,则返回 SymbolDescriptiveString(value)。
    2. string 为 ? ToString(value)。
  3. 如果 NewTarget 是 undefined,则返回 string
  4. 返回 StringCreate(string, ? GetPrototypeFromConstructor(NewTarget, "%String.prototype%"))。

22.1.2 String 构造器的属性

String 构造器:

22.1.2.1 String.fromCharCode ( ...codeUnits )

此函数可以用任意数量的实参调用,这些实参构成 rest 参数 codeUnits

它在被调用时执行以下步骤:

  1. result 为空 String。
  2. codeUnits 的每个元素 next,执行:
    1. nextCU 为数值为 (? ToUint16(next)) 的码元。
    2. result 设置为 resultnextCU字符串连接
  3. 返回 result

此函数的 "length" 属性是 1𝔽

22.1.2.2 String.fromCodePoint ( ...codePoints )

此函数可以用任意数量的实参调用,这些实参构成 rest 参数 codePoints

它在被调用时执行以下步骤:

  1. result 为空 String。
  2. codePoints 的每个元素 next,执行:
    1. nextCP 为 ? ToNumber(next)。
    2. 如果 nextCP 不是整数 Number,则抛出 RangeError 异常。
    3. 如果 (nextCP) < 0 或 (nextCP) > 0x10FFFF,则抛出 RangeError 异常。
    4. result 设置为 resultUTF16EncodeCodePoint((nextCP)) 的字符串连接
  3. 断言:如果 codePoints 为空,则 result 为空 String。
  4. 返回 result

此函数的 "length" 属性是 1𝔽

22.1.2.3 String.prototype

String.prototype 的初始值是 String 原型对象

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

22.1.2.4 String.raw ( template, ...substitutions )

此函数可以用可变数量的实参调用。第一个实参是 template,其余实参构成 List substitutions

它在被调用时执行以下步骤:

  1. substitutionCountsubstitutions 中元素的数量。
  2. cooked 为 ? ToObject(template)。
  3. literals 为 ? ToObject(? Get(cooked, "raw"))。
  4. literalCount 为 ? LengthOfArrayLike(literals)。
  5. 如果 literalCount ≤ 0,则返回空 String。
  6. result 为空 String。
  7. nextIndex 为 0。
  8. 重复,
    1. nextLiteralValue 为 ? Get(literals, ! ToString(𝔽(nextIndex)))。
    2. nextLiteral 为 ? ToString(nextLiteralValue)。
    3. result 设置为 resultnextLiteral字符串连接
    4. 如果 nextIndex + 1 = literalCount,则返回 result
    5. 如果 nextIndex < substitutionCount,则
      1. nextSubValuesubstitutions[nextIndex]。
      2. nextSub 为 ? ToString(nextSubValue)。
      3. result 设置为 resultnextSub字符串连接
    6. nextIndex 设置为 nextIndex + 1。
Note

此函数旨在用作 Tagged Template(13.3.11)的标签函数。当以这种方式调用时,第一个实参将是一个格式良好的模板对象,rest 参数将包含替换值。

22.1.3 String 原型对象的属性

String 原型对象

  • %String.prototype%
  • 是 String 奇异对象,并具有为这类对象指定的内部方法。
  • 具有一个 [[StringData]] 内部槽,其值为空 String。
  • 具有一个 "length" 属性,其初始值为 +0𝔽,并且其特性为 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。
  • 具有一个 [[Prototype]] 内部槽,其值为 %Object.prototype%

除非另有明确说明,下文定义的 String 原型对象的方法不是泛型的,并且传给它们的 this 值必须是 String 值,或是一个具有已初始化为 String 值的 [[StringData]] 内部槽的对象。

22.1.3.1 String.prototype.at ( index )

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. lengthstring 的长度。
  5. relativeIndex 为 ? ToIntegerOrInfinity(index)。
  6. 如果 relativeIndex ≥ 0,则
    1. krelativeIndex
  7. 否则,
    1. klength + relativeIndex
  8. 如果 k < 0 或 klength,则返回 undefined
  9. 返回 stringkk + 1 的子字符串

22.1.3.2 String.prototype.charAt ( position )

Note 1

此方法返回一个单元素 String,包含将此对象转换为 String 后所得 String 值中索引 position 处的码元。如果该索引处没有元素,则结果为空 String。结果是 String 值,而不是 String 对象。

如果 pos 是整数 Number,则 x.charAt(pos) 的结果等价于 x.substring(pos, pos + 1) 的结果。

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. position 设置为 ? ToIntegerOrInfinity(position)。
  5. sizestring 的长度。
  6. 如果 position < 0 或 positionsize,则返回空 String。
  7. 返回 stringpositionposition + 1 的子字符串
Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.3 String.prototype.charCodeAt ( position )

Note 1

此方法返回一个 Number(一个小于 216 的非负整数 Number),它是将此对象转换为 String 后所得 String 中索引 position 处码元的数值。如果该索引处没有元素,则结果为 NaN

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. position 设置为 ? ToIntegerOrInfinity(position)。
  5. sizestring 的长度。
  6. 如果 position < 0 或 positionsize,则返回 NaN
  7. 返回 String string 中索引 position 处码元的数值对应的 Number 值
Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.4 String.prototype.codePointAt ( position )

Note 1

此方法返回一个小于或等于 0x10FFFF𝔽 的非负整数 Number,它是将此对象转换为 String 后所得 String 中索引 position 处的字符串元素开始的 UTF-16 编码码点(6.1.4)的数值。如果该索引处没有元素,则结果为 undefined。如果有效 UTF-16 代理对不是从 position 开始,则结果为 position 处的码元。

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. position 设置为 ? ToIntegerOrInfinity(position)。
  5. sizestring 的长度。
  6. 如果 position < 0 或 positionsize,则返回 undefined
  7. codePointCodePointAt(string, position)。
  8. 返回 𝔽(codePoint.[[CodePoint]])。
Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.5 String.prototype.concat ( ...args )

Note 1

调用此方法时,它返回一个 String 值,该值由 this 值(转换为 String)的码元,后跟每个实参转换为 String 后的码元组成。结果是 String 值,而不是 String 对象。

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. resultstring
  5. args 的每个元素 next,执行:
    1. nextString 为 ? ToString(next)。
    2. result 设置为 resultnextString字符串连接
  6. 返回 result

此方法的 "length" 属性是 1𝔽

Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.6 String.prototype.constructor

String.prototype.constructor 的初始值是 %String%

22.1.3.7 String.prototype.endsWith ( searchString [ , endPosition ] )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. isRegexp 为 ? IsRegExp(searchString)。
  5. 如果 isRegexptrue,则抛出 TypeError 异常。
  6. searchString 设置为 ? ToString(searchString)。
  7. lengthstring 的长度。
  8. 如果 endPositionundefined,则令 positionlength;否则令 position 为 ? ToIntegerOrInfinity(endPosition)。
  9. end 为将 position 夹在 0 与 length 之间的结果。
  10. searchLengthsearchString 的长度。
  11. 如果 searchLength = 0,则返回 true
  12. startend - searchLength
  13. 如果 start < 0,则返回 false
  14. substringstringstartend子字符串
  15. 如果 substringsearchString,则返回 true
  16. 返回 false
Note 1

指定当第一个实参是 RegExp 时抛出异常,是为了允许未来版本定义允许这类实参值的扩展。

Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.8 String.prototype.includes ( searchString [ , position ] )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. isRegexp 为 ? IsRegExp(searchString)。
  5. 如果 isRegexptrue,则抛出 TypeError 异常。
  6. searchString 设置为 ? ToString(searchString)。
  7. positionInt 为 ? ToIntegerOrInfinity(position)。
  8. 断言:如果 positionundefined,则 positionInt 是 0。
  9. lengthstring 的长度。
  10. start 为将 positionInt 夹在 0 与 length 之间的结果。
  11. indexStringIndexOf(string, searchString, start)。
  12. 如果 indexnot-found,则返回 false
  13. 返回 true
Note 1

如果 searchString 作为将此对象转换为 String 的结果的子字符串出现在一个或多个大于或等于 position 的索引处,则此函数返回 true;否则返回 false。如果 positionundefined,则假定为 0,以搜索整个 String。

Note 2

指定当第一个实参是 RegExp 时抛出异常,是为了允许未来版本定义允许这类实参值的扩展。

Note 3

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.9 String.prototype.indexOf ( searchString [ , position ] )

Note 1

如果 searchString 作为将此对象转换为 String 的结果的子字符串出现在一个或多个大于或等于 position 的索引处,则返回这样的最小索引;否则返回 -1𝔽。如果 positionundefined,则假定为 +0𝔽,以搜索整个 String。

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. searchString 设置为 ? ToString(searchString)。
  5. positionInt 为 ? ToIntegerOrInfinity(position)。
  6. 断言:如果 positionundefined,则 positionInt 是 0。
  7. lengthstring 的长度。
  8. start 为将 positionInt 夹在 0 与 length 之间的结果。
  9. resultStringIndexOf(string, searchString, start)。
  10. 如果 resultnot-found,则返回 -1𝔽
  11. 返回 𝔽(result)。
Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.10 String.prototype.isWellFormed ( )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. 返回 IsStringWellFormedUnicode(string)。

22.1.3.11 String.prototype.lastIndexOf ( searchString [ , position ] )

Note 1

如果 searchString 作为将此对象转换为 String 的结果的子字符串出现在一个或多个小于或等于 position 的索引处,则返回这样的最大索引;否则返回 -1𝔽。如果 positionundefined,则假定为 String 值的长度,以搜索整个 String。

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. searchString 设置为 ? ToString(searchString)。
  5. numberPosition 为 ? ToNumber(position)。
  6. 断言:如果 positionundefined,则 numberPositionNaN
  7. 如果 numberPositionNaN,则将 position 设置为 +∞;否则将 position 设置为 ! ToIntegerOrInfinity(numberPosition)。
  8. lengthstring 的长度。
  9. searchLengthsearchString 的长度。
  10. 如果 length < searchLength,则返回 -1𝔽
  11. start 为将 position 夹在 0 与 length - searchLength 之间的结果。
  12. resultStringLastIndexOf(string, searchString, start)。
  13. 如果 resultnot-found,则返回 -1𝔽
  14. 返回 𝔽(result)。
Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.12 String.prototype.localeCompare ( that [ , reserved1 [ , reserved2 ] ] )

包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按 ECMA-402 规范中的规定实现此方法。如果 ECMAScript 实现不包含 ECMA-402 API,则使用此方法的以下规范:

此方法返回一个非 NaN 的 Number,表示对 this 值(转换为 String string)与 that(转换为 String thatValue)执行的实现定义的区域设置敏感 String 比较的结果。该结果旨在对应于根据宿主环境当前区域设置的约定对 String 值排序的顺序;当 string 排在 thatValue 之前时为负,当 string 排在 thatValue 之后时为正,在所有其他情况下为零(表示 stringthatValue 之间没有相对顺序)。

在执行比较之前,此方法执行以下步骤以准备 Strings:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. thatValue 为 ? ToString(that)。

此方法的可选第二和第三参数的含义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得为这些参数位置指定任何其他解释。

实际返回值由实现定义,以允许在其中编码额外信息,但当此方法被视为两个实参的方法时,它必须是一个一致的比较器,定义所有 Strings 集合上的全序。此方法还必须识别并遵守 Unicode Standard 中的规范等价,包括在比较可区分但规范等价的 Strings 时返回 +0𝔽

Note 1

此方法本身不适合直接作为 Array.prototype.sort 的实参,因为后者要求一个二元函数。

Note 2

此方法可以依赖 ECMAScript 环境从宿主环境可获得的任何语言和/或区域设置敏感比较功能,并旨在根据宿主环境当前区域设置的约定进行比较。不过,无论比较能力如何,此方法都必须识别并遵守 Unicode Standard 中的规范等价——例如,以下比较必须全部返回 +0𝔽

// Å ANGSTROM SIGN vs.
// Å LATIN CAPITAL LETTER A + COMBINING RING ABOVE
"\u212B".localeCompare("A\u030A")

// Ω OHM SIGN vs.
// Ω GREEK CAPITAL LETTER OMEGA
"\u2126".localeCompare("\u03A9")

// ṩ LATIN SMALL LETTER S WITH DOT BELOW AND DOT ABOVE vs.
// ṩ LATIN SMALL LETTER S + COMBINING DOT ABOVE + COMBINING DOT BELOW
"\u1E69".localeCompare("s\u0307\u0323")

// ḍ̇ LATIN SMALL LETTER D WITH DOT ABOVE + COMBINING DOT BELOW vs.
// ḍ̇ LATIN SMALL LETTER D WITH DOT BELOW + COMBINING DOT ABOVE
"\u1E0B\u0323".localeCompare("\u1E0D\u0307")

// 가 HANGUL CHOSEONG KIYEOK + HANGUL JUNGSEONG A vs.
// 가 HANGUL SYLLABLE GA
"\u1100\u1161".localeCompare("\uAC00")

关于规范等价的定义和讨论,见 Unicode Standard 第 2 章和第 3 章,以及 Unicode Standard Annex #15, Unicode Normalization FormsUnicode Technical Note #5, Canonical Equivalence in Applications。另见 Unicode Technical Standard #10, Unicode Collation Algorithm

建议此方法不应遵守 Unicode Standard 第 3 章第 3.7 节中定义的 Unicode 兼容等价或兼容分解。

Note 3

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.13 String.prototype.match ( regexpOrPattern )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. 如果 regexpOrPattern 是 Object,则
    1. matcher 为 ? GetMethod(regexpOrPattern, %Symbol.match%)。
    2. 如果 matcher 不是 undefined,则
      1. 返回 ? Call(matcher, regexpOrPattern, « thisValue »)。
  4. string 为 ? ToString(thisValue)。
  5. regexp 为 ? RegExpCreate(regexpOrPattern, undefined)。
  6. 返回 ? Invoke(regexp, %Symbol.match%, « string »)。
Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.14 String.prototype.matchAll ( regexpOrPattern )

此方法对表示 this 值的 String 与 regexpOrPattern 执行正则表达式匹配,并返回一个产生匹配结果的迭代器。每个匹配结果都是一个 Array,其中第一个元素是 String 中被匹配的部分,随后是任何捕获组匹配到的部分。如果正则表达式从未匹配,则返回的迭代器不会产生任何匹配结果。

它在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. 如果 regexpOrPattern 是 Object,则
    1. isRegexp 为 ? IsRegExp(regexpOrPattern)。
    2. 如果 isRegexptrue,则
      1. flags 为 ? Get(regexpOrPattern, "flags")。
      2. 执行 ? RequireObjectCoercible(flags)。
      3. 如果 ? ToString(flags) 不包含 "g",则抛出 TypeError 异常。
    3. matcher 为 ? GetMethod(regexpOrPattern, %Symbol.matchAll%)。
    4. 如果 matcher 不是 undefined,则
      1. 返回 ? Call(matcher, regexpOrPattern, « thisValue »)。
  4. string 为 ? ToString(thisValue)。
  5. regexp 为 ? RegExpCreate(regexpOrPattern, "g")。
  6. 返回 ? Invoke(regexp, %Symbol.matchAll%, « string »)。
Note 1
此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。
Note 2
类似于 String.prototype.splitString.prototype.matchAll 被设计为通常不改变其输入。

22.1.3.15 String.prototype.normalize ( [ form ] )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. 如果 formundefined,则将 form 设置为 "NFC"
  5. 否则,将 form 设置为 ? ToString(form)。
  6. 如果 form 不是 "NFC""NFD""NFKC""NFKD" 之一,则抛出 RangeError 异常。
  7. normal 为 String 值,它是按照 最新 Unicode Standard, Normalization Forms 中指定的方式,把 string 规范化为由 form 命名的规范化形式所得的结果。
  8. 返回 normal
Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.16 String.prototype.padEnd ( maxLength [ , fillString ] )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. 返回 ? StringPaddingBuiltinsImpl(thisValue, maxLength, fillString, end)。

22.1.3.17 String.prototype.padStart ( maxLength [ , fillString ] )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. 返回 ? StringPaddingBuiltinsImpl(thisValue, maxLength, fillString, start)。

22.1.3.17.1 StringPaddingBuiltinsImpl ( thisValue, maxLength, fillString, placement )

The abstract operation StringPaddingBuiltinsImpl takes arguments thisValue (an ECMAScript language value), maxLength (an ECMAScript language value), fillString (an ECMAScript language value), and placement (start or end) and returns either a normal completion containing a String or a throw completion. It performs the following steps when called:

  1. string 为 ? ToString(thisValue)。
  2. intMaxLength(? ToLength(maxLength))。
  3. stringLengthstring 的长度。
  4. 如果 intMaxLengthstringLength,则返回 string
  5. 如果 fillStringundefined,则将 fillString 设置为仅由码元 0x0020(SPACE)组成的 String 值。
  6. 否则,将 fillString 设置为 ? ToString(fillString)。
  7. 返回 StringPad(string, intMaxLength, fillString, placement)。

22.1.3.17.2 StringPad ( string, maxLength, fillString, placement )

The abstract operation StringPad takes arguments string (a String), maxLength (a non-negative integer), fillString (a String), and placement (start or end) and returns a String. It performs the following steps when called:

  1. stringLengthstring 的长度。
  2. 如果 maxLengthstringLength,则返回 string
  3. 如果 fillString 是空 String,则返回 string
  4. fillLengthmaxLength - stringLength
  5. truncatedStringFiller 为由 fillString 的重复连接组成并截断到长度 fillLength 的 String 值。
  6. 如果 placementstart,则返回 truncatedStringFillerstring字符串连接
  7. 返回 stringtruncatedStringFiller字符串连接
Note 1

实参 maxLength 会被夹住,使其不能小于 string 的长度。

Note 2

实参 fillString 默认为 " "(由码元 0x0020 SPACE 组成的 String 值)。

22.1.3.17.3 ToZeroPaddedDecimalString ( n, minLength )

The abstract operation ToZeroPaddedDecimalString takes arguments n (a non-negative integer) and minLength (a non-negative integer) and returns a String. It performs the following steps when called:

  1. stringn 的 String 表示,格式化为十进制数。
  2. 返回 StringPad(string, minLength, "0", start)。

22.1.3.18 String.prototype.repeat ( count )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. n 为 ? ToIntegerOrInfinity(count)。
  5. 如果 n < 0 或 n = +∞,则抛出 RangeError 异常。
  6. 如果 n = 0,则返回空 String。
  7. 返回由 stringn 个副本连接在一起构成的 String 值。
Note 1

此方法创建由 this 值(转换为 String)的码元重复 count 次组成的 String 值。

Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.19 String.prototype.replace ( searchValue, replaceValue )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. 如果 searchValue 是 Object,则
    1. replacer 为 ? GetMethod(searchValue, %Symbol.replace%)。
    2. 如果 replacer 不是 undefined,则
      1. 返回 ? Call(replacer, searchValue, « thisValue, replaceValue »)。
  4. string 为 ? ToString(thisValue)。
  5. searchString 为 ? ToString(searchValue)。
  6. functionalReplaceIsCallable(replaceValue)。
  7. 如果 functionalReplacefalse,则
    1. replaceValue 设置为 ? ToString(replaceValue)。
  8. searchLengthsearchString 的长度。
  9. positionStringIndexOf(string, searchString, 0)。
  10. 如果 positionnot-found,则返回 string
  11. precedingstring 从 0 到 position子字符串
  12. followingstringposition + searchLength 起的子字符串
  13. 如果 functionalReplacetrue,则
    1. replacement 为 ? ToString(? Call(replaceValue, undefined, « searchString, 𝔽(position), string »))。
  14. 否则,
    1. 断言:replaceValue 是 String。
    2. captures 为新的空 List
    3. replacement 为 ! GetSubstitution(searchString, string, position, captures, undefined, replaceValue)。
  15. 返回 precedingreplacementfollowing字符串连接
Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.19.1 GetSubstitution ( matched, string, position, captures, namedCaptures, replacementTemplate )

The abstract operation GetSubstitution takes arguments matched (a String), string (a String), position (a non-negative integer), captures (a List of either Strings or undefined), namedCaptures (an Object or undefined), and replacementTemplate (a String) and returns either a normal completion containing a String or a throw completion. 对于此抽象操作而言,十进制数字是从 0x0030(DIGIT ZERO)到 0x0039(DIGIT NINE)闭区间内的码元。 It performs the following steps when called:

  1. stringLengthstring 的长度。
  2. 断言:positionstringLength
  3. result 为空 String。
  4. templateRemainderreplacementTemplate
  5. 重复,只要 templateRemainder 不是空 String,
    1. 注:以下步骤分离 reftemplateRemainder 的前缀),确定 refReplacement(其替换),然后把该替换追加到 result
    2. 如果 templateRemainder"$$" 开头,则
      1. ref"$$"
      2. refReplacement"$"
    3. 否则,如果 templateRemainder"$`" 开头,则
      1. ref"$`"
      2. refReplacementstring 从 0 到 position子字符串
    4. 否则,如果 templateRemainder"$&" 开头,则
      1. ref"$&"
      2. refReplacementmatched
    5. 否则,如果 templateRemainder"$'"(0x0024(DOLLAR SIGN)后跟 0x0027(APOSTROPHE))开头,则
      1. ref"$'"
      2. matchLengthmatched 的长度。
      3. tailPositionposition + matchLength
      4. refReplacementstringmin(tailPosition, stringLength) 起的子字符串
      5. 注:只有当此抽象操作%RegExp.prototype% 的固有 %Symbol.replace% 方法在其 "exec" 属性不是固有 %RegExp.prototype.exec% 的对象上调用时,tailPosition 才可能超过 stringLength
    6. 否则,如果 templateRemainder"$" 后跟 1 个或更多十进制数字开头,则
      1. 如果 templateRemainder"$" 后跟 2 个或更多十进制数字开头,则令 digitCount 为 2;否则令 digitCount 为 1。
      2. digitstemplateRemainder 从 1 到 1 + digitCount子字符串
      3. index(StringToNumber(digits))。
      4. 断言:0 ≤ index ≤ 99。
      5. captureLengthcaptures 中元素的数量。
      6. 如果 index > captureLengthdigitCount = 2,则
        1. 注:当两位替换模式指定了超出捕获组数量的索引时,它会被视为一位替换模式,后跟一个字面数字。
        2. digitCount 设置为 1。
        3. digits 设置为 digits 从 0 到 1 的子字符串
        4. index 设置为 (StringToNumber(digits))。
      7. reftemplateRemainder 从 0 到 1 + digitCount子字符串
      8. 如果 1 ≤ indexcaptureLength,则
        1. capturecaptures[index - 1]。
        2. 如果 captureundefined,则
          1. refReplacement 为空 String。
        3. 否则,
          1. refReplacementcapture
      9. 否则,
        1. refReplacementref
    7. 否则,如果 templateRemainder"$<" 开头,则
      1. gtPositionStringIndexOf(templateRemainder, ">", 0)。
      2. 如果 gtPositionnot-foundnamedCapturesundefined,则
        1. ref"$<"
        2. refReplacementref
      3. 否则,
        1. reftemplateRemainder 从 0 到 gtPosition + 1 的子字符串
        2. groupNametemplateRemainder 从 2 到 gtPosition子字符串
        3. 断言:namedCaptures 是 Object。
        4. capture 为 ? Get(namedCaptures, groupName)。
        5. 如果 captureundefined,则
          1. refReplacement 为空 String。
        6. 否则,
          1. refReplacement 为 ? ToString(capture)。
    8. 否则,
      1. reftemplateRemainder 从 0 到 1 的子字符串
      2. refReplacementref
    9. refLengthref 的长度。
    10. templateRemainder 设置为 templateRemainderrefLength 起的子字符串
    11. result 设置为 resultrefReplacement字符串连接
  6. 返回 result

22.1.3.20 String.prototype.replaceAll ( searchValue, replaceValue )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. 如果 searchValue 是 Object,则
    1. isRegexp 为 ? IsRegExp(searchValue)。
    2. 如果 isRegexptrue,则
      1. flags 为 ? Get(searchValue, "flags")。
      2. 执行 ? RequireObjectCoercible(flags)。
      3. 如果 ? ToString(flags) 不包含 "g",则抛出 TypeError 异常。
    3. replacer 为 ? GetMethod(searchValue, %Symbol.replace%)。
    4. 如果 replacer 不是 undefined,则
      1. 返回 ? Call(replacer, searchValue, « thisValue, replaceValue »)。
  4. string 为 ? ToString(thisValue)。
  5. searchString 为 ? ToString(searchValue)。
  6. functionalReplaceIsCallable(replaceValue)。
  7. 如果 functionalReplacefalse,则
    1. replaceValue 设置为 ? ToString(replaceValue)。
  8. searchLengthsearchString 的长度。
  9. advanceBymax(1, searchLength)。
  10. matchPositions 为新的空 List
  11. positionStringIndexOf(string, searchString, 0)。
  12. 重复,只要 position 不是 not-found
    1. position 追加到 matchPositions
    2. position 设置为 StringIndexOf(string, searchString, position + advanceBy)。
  13. endOfLastMatch 为 0。
  14. result 为空 String。
  15. matchPositions 的每个元素 matchPosition,执行:
    1. preservedstringendOfLastMatchmatchPosition子字符串
    2. 如果 functionalReplacetrue,则
      1. replacement 为 ? ToString(? Call(replaceValue, undefined, « searchString, 𝔽(matchPosition), string »))。
    3. 否则,
      1. 断言:replaceValue 是 String。
      2. captures 为新的空 List
      3. replacement 为 ! GetSubstitution(searchString, string, matchPosition, captures, undefined, replaceValue)。
    4. result 设置为 resultpreservedreplacement字符串连接
    5. endOfLastMatch 设置为 matchPosition + searchLength
  16. 如果 endOfLastMatch < string 的长度,则
    1. result 设置为 resultstringendOfLastMatch 起的子字符串字符串连接
  17. 返回 result

22.1.3.21 String.prototype.search ( regexpOrPattern )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. 如果 regexpOrPattern 是 Object,则
    1. searcher 为 ? GetMethod(regexpOrPattern, %Symbol.search%)。
    2. 如果 searcher 不是 undefined,则
      1. 返回 ? Call(searcher, regexpOrPattern, « thisValue »)。
  4. string 为 ? ToString(thisValue)。
  5. regexp 为 ? RegExpCreate(regexpOrPattern, undefined)。
  6. 返回 ? Invoke(regexp, %Symbol.search%, « string »)。
Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.22 String.prototype.slice ( start, end )

此方法返回将此对象转换为 String 后所得结果的一个子字符串,从索引 start 开始,并持续到但不包括索引 end(如果 endundefined,则持续到 String 末尾)。如果 start 为负,则将其视为 sourceLength + start,其中 sourceLength 是 String 的长度。如果 end 为负,则将其视为 sourceLength + end,其中 sourceLength 是 String 的长度。结果是 String 值,而不是 String 对象。

它在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. lengthstring 的长度。
  5. intStart 为 ? ToIntegerOrInfinity(start)。
  6. 如果 intStart = -∞,则令 from 为 0。
  7. 否则,如果 intStart < 0,则令 frommax(length + intStart, 0)。
  8. 否则,令 frommin(intStart, length)。
  9. 如果 endundefined,则令 intEndlength;否则令 intEnd 为 ? ToIntegerOrInfinity(end)。
  10. 如果 intEnd = -∞,则令 to 为 0。
  11. 否则,如果 intEnd < 0,则令 tomax(length + intEnd, 0)。
  12. 否则,令 tomin(intEnd, length)。
  13. 如果 fromto,则返回空 String。
  14. 返回 stringfromto子字符串
Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.23 String.prototype.split ( separator, limit )

此方法返回一个 Array,其中存储了将此对象转换为 String 后所得结果的各个子字符串。这些子字符串通过从左到右搜索 separator 的出现位置来确定;这些出现位置不是返回数组中任何 String 的一部分,而是用来分割该 String 值。separator 的值可以是任意长度的 String,也可以是具有 %Symbol.split% 方法的对象,例如 RegExp。

它在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. 如果 separator 是 Object,则
    1. splitter 为 ? GetMethod(separator, %Symbol.split%)。
    2. 如果 splitter 不是 undefined,则
      1. 返回 ? Call(splitter, separator, « thisValue, limit »)。
  4. string 为 ? ToString(thisValue)。
  5. 如果 limitundefined,则令 lim 为 232 - 1;否则令 lim(? ToUint32(limit))。
  6. separatorString 为 ? ToString(separator)。
  7. 如果 lim = 0,则
    1. 返回 CreateArrayFromList(« »)。
  8. 如果 separatorundefined,则
    1. 返回 CreateArrayFromListstring »)。
  9. separatorLengthseparatorString 的长度。
  10. 如果 separatorLength = 0,则
    1. stringLengthstring 的长度。
    2. outLength 为将 lim 夹在 0 与 stringLength 之间的结果。
    3. headstring 从 0 到 outLength子字符串
    4. codeUnits 为由 head 的各元素码元序列组成的 List
    5. 返回 CreateArrayFromList(codeUnits)。
  11. 如果 string 是空 String,则返回 CreateArrayFromListstring »)。
  12. substrings 为新的空 List
  13. searchStart 为 0。
  14. matchIndexStringIndexOf(string, separatorString, 0)。
  15. 重复,只要 matchIndex 不是 not-found
    1. substringstringsearchStartmatchIndex子字符串
    2. substring 追加到 substrings
    3. 如果 substrings 中元素的数量是 lim,则返回 CreateArrayFromList(substrings)。
    4. searchStart 设置为 matchIndex + separatorLength
    5. matchIndex 设置为 StringIndexOf(string, separatorString, searchStart)。
  16. substringstringsearchStart 起的子字符串
  17. substring 追加到 substrings
  18. 返回 CreateArrayFromList(substrings)。
Note 1

separator 的值可以是空 String。在这种情况下,separator 不匹配输入 String 开头或结尾处的空子字符串,也不匹配上一个分隔符匹配末尾处的空子字符串。如果 separator 是空 String,则 String 会被拆分为单个码元元素;结果数组的长度等于 String 的长度,并且每个子字符串包含一个码元。

如果 this 值是(或转换为)空 String,则结果取决于 separator 是否能匹配空 String。如果可以,则结果数组不包含元素。否则,结果数组包含一个元素,即空 String。

如果 separatorundefined,则结果数组只包含一个 String,即 this 值(转换为 String)。如果 limit 不是 undefined,则输出数组会被截断,使其包含不超过 limit 个元素。

Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.24 String.prototype.startsWith ( searchString [ , position ] )

此方法在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. isRegexp 为 ? IsRegExp(searchString)。
  5. 如果 isRegexptrue,则抛出 TypeError 异常。
  6. searchString 设置为 ? ToString(searchString)。
  7. lengthstring 的长度。
  8. 如果 positionundefined,则将 position 设置为 0;否则将 position 设置为 ? ToIntegerOrInfinity(position)。
  9. start 为将 position 夹在 0 与 length 之间的结果。
  10. searchLengthsearchString 的长度。
  11. 如果 searchLength = 0,则返回 true
  12. endstart + searchLength
  13. 如果 end > length,则返回 false
  14. substringstringstartend子字符串
  15. 如果 substringsearchString,则返回 true
  16. 返回 false
Note 1

指定当第一个实参是 RegExp 时抛出异常,是为了允许未来版本定义允许这类实参值的扩展。

Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.25 String.prototype.substring ( start, end )

此方法返回将此对象转换为 String 后所得结果的一个子字符串,从索引 start 开始,并持续到但不包括 String 的索引 end(如果 endundefined,则持续到 String 末尾)。结果是 String 值,而不是 String 对象。

如果任一实参是 NaN 或负数,则将其替换为零;如果任一实参严格大于 String 的长度,则将其替换为 String 的长度。

如果 start 严格大于 end,则交换二者。

它在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. lengthstring 的长度。
  5. intStart 为 ? ToIntegerOrInfinity(start)。
  6. 如果 endundefined,则令 intEndlength;否则令 intEnd 为 ? ToIntegerOrInfinity(end)。
  7. finalStart 为将 intStart 夹在 0 与 length 之间的结果。
  8. finalEnd 为将 intEnd 夹在 0 与 length 之间的结果。
  9. frommin(finalStart, finalEnd)。
  10. tomax(finalStart, finalEnd)。
  11. 返回 stringfromto子字符串
Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.26 String.prototype.toLocaleLowerCase ( [ reserved1 [ , reserved2 ] ] )

包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按 ECMA-402 规范中的规定实现此方法。如果 ECMAScript 实现不包含 ECMA-402 API,则使用此方法的以下规范:

此方法将 String 值解释为 UTF-16 编码码点序列,如 6.1.4 中所述。

它的工作方式与 toLowerCase 完全相同,只是它旨在产生与宿主环境当前区域设置的约定相对应的区域设置敏感结果。只有在少数情况(例如土耳其语)中,该语言的规则与常规 Unicode 大小写映射冲突时,才会存在差异。

此方法可选参数的含义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得把这些参数位置用于任何其他用途。

Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.27 String.prototype.toLocaleUpperCase ( [ reserved1 [ , reserved2 ] ] )

包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按 ECMA-402 规范中的规定实现此方法。如果 ECMAScript 实现不包含 ECMA-402 API,则使用此方法的以下规范:

此方法将 String 值解释为 UTF-16 编码码点序列,如 6.1.4 中所述。

它的工作方式与 toUpperCase 完全相同,只是它旨在产生与宿主环境当前区域设置的约定相对应的区域设置敏感结果。只有在少数情况(例如土耳其语)中,该语言的规则与常规 Unicode 大小写映射冲突时,才会存在差异。

此方法可选参数的含义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得把这些参数位置用于任何其他用途。

Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.28 String.prototype.toLowerCase ( )

此方法将 String 值解释为 UTF-16 编码码点序列,如 6.1.4 中所述。

它在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. sTextStringToCodePoints(string)。
  5. lowerText 为按照 Unicode Default Case Conversion 算法对 sText 执行 toLowercase 的结果。
  6. lowercaseStringCodePointsToString(lowerText)。
  7. 返回 lowercaseString

结果必须根据 Unicode Character Database 中的区域设置无关大小写映射派生(这明确不仅包括文件 UnicodeData.txt,还包括随附文件 SpecialCasing.txt 中所有区域设置无关的映射)。

Note 1

某些码点的大小写映射可能产生多个码点。在这种情况下,结果 String 的长度可能与源 String 不同。由于 toUpperCasetoLowerCase 都具有上下文敏感行为,这两个方法并不对称。换句话说,s.toUpperCase().toLowerCase() 不一定等于 s.toLowerCase()

Note 2

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.29 String.prototype.toString ( )

此方法在被调用时执行以下步骤:

  1. 返回 ? ThisStringValue(this value)。
Note

对于 String 对象,此方法恰好返回与 valueOf 方法相同的内容。

22.1.3.30 String.prototype.toUpperCase ( )

此方法将 String 值解释为 UTF-16 编码码点序列,如 6.1.4 中所述。

它的行为与 String.prototype.toLowerCase 完全相同,只是 String 使用 Unicode Default Case Conversion 的 toUppercase 算法进行映射。

Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.31 String.prototype.toWellFormed ( )

此方法返回此对象的 String 表示,其中所有不属于代理对的前导代理和尾随代理都会被替换为 U+FFFD(REPLACEMENT CHARACTER)。

它在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. string 为 ? ToString(thisValue)。
  4. stringLengthstring 的长度。
  5. k 为 0。
  6. result 为空 String。
  7. 重复,只要 k < stringLength
    1. codePointCodePointAt(string, k)。
    2. 如果 codePoint.[[IsUnpairedSurrogate]]true,则
      1. result 设置为 result 和 0xFFFD(REPLACEMENT CHARACTER)的字符串连接
    3. 否则,
      1. result 设置为 resultUTF16EncodeCodePoint(codePoint.[[CodePoint]]) 的字符串连接
    4. k 设置为 k + codePoint.[[CodeUnitCount]]
  8. 返回 result

22.1.3.32 String.prototype.trim ( )

此方法将 String 值解释为 UTF-16 编码码点序列,如 6.1.4 中所述。

它在被调用时执行以下步骤:

  1. thisValuethis value。
  2. 返回 ? TrimString(thisValue, start+end)。
Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.32.1 TrimString ( arg, where )

The abstract operation TrimString takes arguments arg (an ECMAScript language value) and where (start, end, or start+end) and returns either a normal completion containing a String or a throw completion. 它将 arg 解释为 UTF-16 编码码点序列,如 6.1.4 中所述。 It performs the following steps when called:

  1. 执行 ? RequireObjectCoercible(arg)。
  2. string 为 ? ToString(arg)。
  3. 如果 wherestart,则
    1. trimmedStringstring 的副本并移除其前导空白。
  4. 否则,如果 whereend,则
    1. trimmedStringstring 的副本并移除其尾随空白。
  5. 否则,
    1. 断言:wherestart+end
    2. trimmedStringstring 的副本并移除其前导和尾随空白。
  6. 返回 trimmedString

空白的定义是 WhiteSpaceLineTerminator 的并集。在确定 Unicode 码点是否属于 Unicode 通用类别 “Space_Separator”(“Zs”)时,码元序列会按 6.1.4 中指定的方式解释为 UTF-16 编码码点序列。

22.1.3.33 String.prototype.trimEnd ( )

此方法将 String 值解释为 UTF-16 编码码点序列,如 6.1.4 中所述。

它在被调用时执行以下步骤:

  1. stringthis value。
  2. 返回 ? TrimString(string, end)。
Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.34 String.prototype.trimStart ( )

此方法将 String 值解释为 UTF-16 编码码点序列,如 6.1.4 中所述。

它在被调用时执行以下步骤:

  1. stringthis value。
  2. 返回 ? TrimString(string, start)。
Note

此方法有意设计为泛型的;它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

22.1.3.35 String.prototype.valueOf ( )

此方法在被调用时执行以下步骤:

  1. 返回 ? ThisStringValue(this value)。

22.1.3.35.1 ThisStringValue ( arg )

The abstract operation ThisStringValue takes argument arg (an ECMAScript language value) and returns either a normal completion containing a String or a throw completion. It performs the following steps when called:

  1. 如果 arg 是 String,则返回 arg
  2. 如果 arg 是 Object 且 arg 具有 [[StringData]] 内部槽,则
    1. stringarg.[[StringData]]
    2. 断言:string 是 String。
    3. 返回 string
  3. 抛出 TypeError 异常。

22.1.3.36 String.prototype [ %Symbol.iterator% ] ( )

此方法返回一个迭代器对象,该对象迭代 String 值的码点,并将每个码点作为 String 值返回。

它在被调用时执行以下步骤:

  1. stringthis value。
  2. 执行 ? RequireObjectCoercible(string)。
  3. string 设置为 ? ToString(string)。
  4. closure 为一个新的无参数 Abstract Closure,它捕获 string,并在被调用时执行以下步骤:
    1. lengthstring 的长度。
    2. position 为 0。
    3. 重复,只要 position < length
      1. codePointCodePointAt(string, position)。
      2. nextIndexposition + codePoint.[[CodeUnitCount]]
      3. resultStringstringpositionnextIndex子字符串
      4. position 设置为 nextIndex
      5. 执行 ? GeneratorYield(CreateIteratorResultObject(resultString, false))。
    4. 返回 NormalCompletion(unused)。
  5. 返回 CreateIteratorFromClosure(closure, "%StringIteratorPrototype%", %StringIteratorPrototype%)。

此方法的 "name" 属性的值是 "[Symbol.iterator]"

22.1.4 String 实例的属性

String 实例是 String 奇异对象,并具有为这类对象指定的内部方法。String 实例继承自 String 原型对象的属性。String 实例还具有一个 [[StringData]] 内部槽。[[StringData]] 内部槽是此 String 对象所表示的 String 值。

String 实例具有一个 "length" 属性,以及一组具有整数索引名称的可枚举属性。

22.1.4.1 length

此 String 对象所表示的 String 值中的元素数量。

一旦 String 对象被初始化,此属性就不再变化。它具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

22.1.5 String Iterator 对象

String Iterator 是表示对某个特定 String 实例对象的某次特定迭代的对象。String Iterator 对象没有具名构造器。相反,String Iterator 对象通过调用 String 实例对象的某些方法创建。

22.1.5.1 %StringIteratorPrototype% 对象

%StringIteratorPrototype% 对象:

22.1.5.1.1 %StringIteratorPrototype%.next ( )

  1. 返回 ? GeneratorResume(this value, empty, "%StringIteratorPrototype%")。

22.1.5.1.2 %StringIteratorPrototype% [ %Symbol.toStringTag% ]

%Symbol.toStringTag% 属性的初始值是 String 值 "String Iterator"

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }。

22.2 RegExp(正则表达式)对象

RegExp 对象包含一个正则表达式以及相关联的标志。

Note

正则表达式的形式和功能以 Perl 5 编程语言中的正则表达式设施为模型。

22.2.1 模式

RegExp 构造器会把以下文法应用于输入的 pattern String。如果该文法不能把该 String 解释为 Pattern 的展开,则会发生错误。

语法

Pattern[UnicodeMode, UnicodeSetsMode, NamedCaptureGroups] :: Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] Disjunction[UnicodeMode, UnicodeSetsMode, NamedCaptureGroups] :: Alternative[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] Alternative[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] | Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] Alternative[UnicodeMode, UnicodeSetsMode, NamedCaptureGroups] :: [empty] Alternative[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] Term[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] Term[UnicodeMode, UnicodeSetsMode, NamedCaptureGroups] :: Assertion[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] Atom[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] Atom[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] Quantifier Assertion[UnicodeMode, UnicodeSetsMode, NamedCaptureGroups] :: ^ $ \b \B (?= Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) (?! Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) (?<= Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) (?<! Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) Quantifier :: QuantifierPrefix QuantifierPrefix ? QuantifierPrefix :: * + ? { DecimalDigits[~Sep] } { DecimalDigits[~Sep] ,} { DecimalDigits[~Sep] , DecimalDigits[~Sep] } Atom[UnicodeMode, UnicodeSetsMode, NamedCaptureGroups] :: PatternCharacter . \ AtomEscape[?UnicodeMode, ?NamedCaptureGroups] CharacterClass[?UnicodeMode, ?UnicodeSetsMode] ( GroupSpecifier[?UnicodeMode]opt Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) (? RegularExpressionModifiers : Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) (? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) RegularExpressionModifiers :: [empty] RegularExpressionModifiers RegularExpressionModifier RegularExpressionModifier :: one of i m s SyntaxCharacter :: one of ^ $ \ . * + ? ( ) [ ] { } | PatternCharacter :: SourceCharacter but not SyntaxCharacter AtomEscape[UnicodeMode, NamedCaptureGroups] :: DecimalEscape CharacterClassEscape[?UnicodeMode] CharacterEscape[?UnicodeMode] [+NamedCaptureGroups] k GroupName[?UnicodeMode] CharacterEscape[UnicodeMode] :: ControlEscape c AsciiLetter 0 [lookahead ∉ DecimalDigit] HexEscapeSequence RegExpUnicodeEscapeSequence[?UnicodeMode] IdentityEscape[?UnicodeMode] ControlEscape :: one of f n r t v GroupSpecifier[UnicodeMode] :: ? GroupName[?UnicodeMode] GroupName[UnicodeMode] :: < RegExpIdentifierName[?UnicodeMode] > RegExpIdentifierName[UnicodeMode] :: RegExpIdentifierStart[?UnicodeMode] RegExpIdentifierName[?UnicodeMode] RegExpIdentifierPart[?UnicodeMode] RegExpIdentifierStart[UnicodeMode] :: IdentifierStartChar \ RegExpUnicodeEscapeSequence[+UnicodeMode] [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate RegExpIdentifierPart[UnicodeMode] :: IdentifierPartChar \ RegExpUnicodeEscapeSequence[+UnicodeMode] [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate RegExpUnicodeEscapeSequence[UnicodeMode] :: [+UnicodeMode] u HexLeadSurrogate \u HexTrailSurrogate [+UnicodeMode] u HexLeadSurrogate [+UnicodeMode] u HexTrailSurrogate [+UnicodeMode] u HexNonSurrogate [~UnicodeMode] u Hex4Digits [+UnicodeMode] u{ CodePoint } UnicodeLeadSurrogate :: any Unicode code point in the inclusive interval from U+D800 to U+DBFF UnicodeTrailSurrogate :: any Unicode code point in the inclusive interval from U+DC00 to U+DFFF

对于每个 \u HexTrailSurrogate,如果其相关联的 u HexLeadSurrogate 的选择存在歧义,则应将其关联到最近的可能 u HexLeadSurrogate,且该 u HexLeadSurrogate 否则就没有对应的 \u HexTrailSurrogate

HexLeadSurrogate :: Hex4Digits but only if the MV of Hex4Digits is in the inclusive interval from 0xD800 to 0xDBFF HexTrailSurrogate :: Hex4Digits but only if the MV of Hex4Digits is in the inclusive interval from 0xDC00 to 0xDFFF HexNonSurrogate :: Hex4Digits but only if the MV of Hex4Digits is not in the inclusive interval from 0xD800 to 0xDFFF IdentityEscape[UnicodeMode] :: [+UnicodeMode] SyntaxCharacter [+UnicodeMode] / [~UnicodeMode] SourceCharacter but not UnicodeIDContinue DecimalEscape :: NonZeroDigit DecimalDigits[~Sep]opt [lookahead ∉ DecimalDigit] CharacterClassEscape[UnicodeMode] :: d D s S w W [+UnicodeMode] p{ UnicodePropertyValueExpression } [+UnicodeMode] P{ UnicodePropertyValueExpression } UnicodePropertyValueExpression :: UnicodePropertyName = UnicodePropertyValue LoneUnicodePropertyNameOrValue UnicodePropertyName :: UnicodePropertyNameCharacters UnicodePropertyNameCharacters :: UnicodePropertyNameCharacter UnicodePropertyNameCharactersopt UnicodePropertyValue :: UnicodePropertyValueCharacters LoneUnicodePropertyNameOrValue :: UnicodePropertyValueCharacters UnicodePropertyValueCharacters :: UnicodePropertyValueCharacter UnicodePropertyValueCharactersopt UnicodePropertyValueCharacter :: UnicodePropertyNameCharacter DecimalDigit UnicodePropertyNameCharacter :: AsciiLetter _ CharacterClass[UnicodeMode, UnicodeSetsMode] :: [ [lookahead ≠ ^] ClassContents[?UnicodeMode, ?UnicodeSetsMode] ] [^ ClassContents[?UnicodeMode, ?UnicodeSetsMode] ] ClassContents[UnicodeMode, UnicodeSetsMode] :: [empty] [~UnicodeSetsMode] NonemptyClassRanges[?UnicodeMode] [+UnicodeSetsMode] ClassSetExpression NonemptyClassRanges[UnicodeMode] :: ClassAtom[?UnicodeMode] ClassAtom[?UnicodeMode] NonemptyClassRangesNoDash[?UnicodeMode] ClassAtom[?UnicodeMode] - ClassAtom[?UnicodeMode] ClassContents[?UnicodeMode, ~UnicodeSetsMode] NonemptyClassRangesNoDash[UnicodeMode] :: ClassAtom[?UnicodeMode] ClassAtomNoDash[?UnicodeMode] NonemptyClassRangesNoDash[?UnicodeMode] ClassAtomNoDash[?UnicodeMode] - ClassAtom[?UnicodeMode] ClassContents[?UnicodeMode, ~UnicodeSetsMode] ClassAtom[UnicodeMode] :: - ClassAtomNoDash[?UnicodeMode] ClassAtomNoDash[UnicodeMode] :: SourceCharacter but not one of \ or ] or - \ ClassEscape[?UnicodeMode] ClassEscape[UnicodeMode] :: b [+UnicodeMode] - CharacterClassEscape[?UnicodeMode] CharacterEscape[?UnicodeMode] ClassSetExpression :: ClassUnion ClassIntersection ClassSubtraction ClassUnion :: ClassSetRange ClassUnionopt ClassSetOperand ClassUnionopt ClassIntersection :: ClassSetOperand && [lookahead ≠ &] ClassSetOperand ClassIntersection && [lookahead ≠ &] ClassSetOperand ClassSubtraction :: ClassSetOperand -- ClassSetOperand ClassSubtraction -- ClassSetOperand ClassSetRange :: ClassSetCharacter - ClassSetCharacter ClassSetOperand :: NestedClass ClassStringDisjunction ClassSetCharacter NestedClass :: [ [lookahead ≠ ^] ClassContents[+UnicodeMode, +UnicodeSetsMode] ] [^ ClassContents[+UnicodeMode, +UnicodeSetsMode] ] \ CharacterClassEscape[+UnicodeMode] Note 1

这里的前两行等价于 CharacterClass。

ClassStringDisjunction :: \q{ ClassStringDisjunctionContents } ClassStringDisjunctionContents :: ClassString ClassString | ClassStringDisjunctionContents ClassString :: [empty] NonEmptyClassString NonEmptyClassString :: ClassSetCharacter NonEmptyClassStringopt ClassSetCharacter :: [lookahead ∉ ClassSetReservedDoublePunctuator] SourceCharacter but not ClassSetSyntaxCharacter \ CharacterEscape[+UnicodeMode] \ ClassSetReservedPunctuator \b ClassSetReservedDoublePunctuator :: one of && !! ## $$ %% ** ++ ,, .. :: ;; << == >> ?? @@ ^^ `` ~~ ClassSetSyntaxCharacter :: one of ( ) [ ] { } / - \ | ClassSetReservedPunctuator :: one of & - ! # % , : ; < = > @ ` ~ Note 2

本节中的若干产生式在 B.1.2 一节中给出了替代定义。

22.2.1.1 Static Semantics: 早期错误

Note

本节在 B.1.2.1 中被修订。

Pattern :: Disjunction QuantifierPrefix :: { DecimalDigits , DecimalDigits } Atom :: (? RegularExpressionModifiers : Disjunction ) Atom :: (? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction ) AtomEscape :: k GroupName AtomEscape :: DecimalEscape NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents NonemptyClassRangesNoDash :: ClassAtomNoDash - ClassAtom ClassContents RegExpIdentifierStart :: \ RegExpUnicodeEscapeSequence RegExpIdentifierStart :: UnicodeLeadSurrogate UnicodeTrailSurrogate RegExpIdentifierPart :: \ RegExpUnicodeEscapeSequence RegExpIdentifierPart :: UnicodeLeadSurrogate UnicodeTrailSurrogate UnicodePropertyValueExpression :: UnicodePropertyName = UnicodePropertyValue UnicodePropertyValueExpression :: LoneUnicodePropertyNameOrValue CharacterClassEscape :: P{ UnicodePropertyValueExpression } CharacterClass :: [^ ClassContents ] NestedClass :: [^ ClassContents ] ClassSetRange :: ClassSetCharacter - ClassSetCharacter

22.2.1.2 Static Semantics: CountLeftCapturingParensWithin ( parseNode )

The abstract operation CountLeftCapturingParensWithin takes argument parseNode (a Parse Node) and returns a non-negative integer. 它返回 parseNode 中左捕获括号的数量。左捕获括号是被 Atom :: ( GroupSpecifieropt Disjunction ) 产生式的 ( 终结符匹配的任意 ( 模式字符。

Note

本节在 B.1.2.2 中被修订。

It performs the following steps when called:

  1. 断言:parseNodeRegExp Pattern 文法中某个产生式的实例。
  2. 返回 parseNode包含 Atom :: ( GroupSpecifieropt Disjunction ) Parse Node 的数量。

22.2.1.3 Static Semantics: CountLeftCapturingParensBefore ( parseNode )

The abstract operation CountLeftCapturingParensBefore takes argument parseNode (a Parse Node) and returns a non-negative integer. 它返回外围 pattern 中位于 parseNode 左侧的左捕获括号数量。

Note

本节在 B.1.2.2 中被修订。

It performs the following steps when called:

  1. 断言:parseNodeRegExp Pattern 文法中某个产生式的实例。
  2. pattern包含 parseNodePattern
  3. 返回 pattern包含的、出现在 parseNode 之前或包含 parseNode Atom :: ( GroupSpecifieropt Disjunction ) Parse Node 的数量。

22.2.1.4 Static Semantics: MightBothParticipate ( x, y )

The abstract operation MightBothParticipate takes arguments x (a Parse Node) and y (a Parse Node) and returns a Boolean. It performs the following steps when called:

  1. 断言:xy 具有相同的外围 Pattern
  2. 如果外围 Pattern 包含一个 Disjunction :: Alternative | Disjunction Parse Node,使得 x 包含Alternative 内而 y 包含在派生的 Disjunction 内,或者 x 包含在派生的 Disjunction 内而 y 包含Alternative 内,则返回 false
  3. 返回 true

22.2.1.5 Static Semantics: CapturingGroupNumber

The syntax-directed operation CapturingGroupNumber takes no arguments and returns a positive integer.

Note

本节在 B.1.2.1 中被修订。

It is defined piecewise over the following productions:

DecimalEscape :: NonZeroDigit
  1. 返回 NonZeroDigit 的 MV。
DecimalEscape :: NonZeroDigit DecimalDigits
  1. nDecimalDigits 中码点的数量。
  2. 返回(NonZeroDigit 的 MV × 10n 加上 DecimalDigits 的 MV)。

NonZeroDigit 的 MV”和“DecimalDigits 的 MV”的定义见 12.9.3

22.2.1.6 Static Semantics: IsCharacterClass

The syntax-directed operation IsCharacterClass takes no arguments and returns a Boolean.

Note

本节在 B.1.2.3 中被修订。

It is defined piecewise over the following productions:

ClassAtom :: - ClassAtomNoDash :: SourceCharacter but not one of \ or ] or - ClassEscape :: b - CharacterEscape
  1. 返回 false
ClassEscape :: CharacterClassEscape
  1. 返回 true

22.2.1.7 Static Semantics: CharacterValue

The syntax-directed operation CharacterValue takes no arguments and returns a non-negative integer.

Note 1

本节在 B.1.2.4 中被修订。

It is defined piecewise over the following productions:

ClassAtom :: -
  1. 返回 U+002D(HYPHEN-MINUS)的数值。
ClassAtomNoDash :: SourceCharacter but not one of \ or ] or -
  1. codePointSourceCharacter 匹配的码点。
  2. 返回 codePoint 的数值。
ClassEscape :: b
  1. 返回 U+0008(BACKSPACE)的数值。
ClassEscape :: -
  1. 返回 U+002D(HYPHEN-MINUS)的数值。
CharacterEscape :: ControlEscape
  1. 返回依据 Table 63 得到的数值。
Table 63: ControlEscape 码点值
ControlEscape 数值 码点 Unicode 名称 符号
t 9 U+0009 CHARACTER TABULATION <HT>
n 10 U+000A LINE FEED (LF) <LF>
v 11 U+000B LINE TABULATION <VT>
f 12 U+000C FORM FEED (FF) <FF>
r 13 U+000D CARRIAGE RETURN (CR) <CR>
CharacterEscape :: c AsciiLetter
  1. codePointAsciiLetter 匹配的码点。
  2. icodePoint 的数值。
  3. 返回 i 除以 32 的余数。
CharacterEscape :: 0 [lookahead ∉ DecimalDigit]
  1. 返回 U+0000(NULL)的数值。
Note 2

\0 表示 <NUL> 字符,并且其后不能跟十进制数字。

CharacterEscape :: HexEscapeSequence
  1. 返回 HexEscapeSequence 的 MV。
RegExpUnicodeEscapeSequence :: u HexLeadSurrogate \u HexTrailSurrogate
  1. leadHexLeadSurrogateCharacterValue
  2. trailHexTrailSurrogateCharacterValue
  3. codePointUTF16SurrogatePairToCodePoint(lead, trail)。
  4. 返回 codePoint 的数值。
RegExpUnicodeEscapeSequence :: u Hex4Digits
  1. 返回 Hex4Digits 的 MV。
RegExpUnicodeEscapeSequence :: u{ CodePoint }
  1. 返回 CodePoint 的 MV。
HexLeadSurrogate :: Hex4Digits HexTrailSurrogate :: Hex4Digits HexNonSurrogate :: Hex4Digits
  1. 返回 Hex4Digits 的 MV。
CharacterEscape :: IdentityEscape
  1. codePointIdentityEscape 匹配的码点。
  2. 返回 codePoint 的数值。
ClassSetCharacter :: SourceCharacter but not ClassSetSyntaxCharacter
  1. codePointSourceCharacter 匹配的码点。
  2. 返回 codePoint 的数值。
ClassSetCharacter :: \ ClassSetReservedPunctuator
  1. codePointClassSetReservedPunctuator 匹配的码点。
  2. 返回 codePoint 的数值。
ClassSetCharacter :: \b
  1. 返回 U+0008(BACKSPACE)的数值。

22.2.1.8 Static Semantics: MayContainStrings

The syntax-directed operation MayContainStrings takes no arguments and returns a Boolean. It is defined piecewise over the following productions:

CharacterClassEscape :: d D s S w W P{ UnicodePropertyValueExpression } UnicodePropertyValueExpression :: UnicodePropertyName = UnicodePropertyValue NestedClass :: [^ ClassContents ] ClassContents :: [empty] NonemptyClassRanges ClassSetOperand :: ClassSetCharacter
  1. 返回 false
UnicodePropertyValueExpression :: LoneUnicodePropertyNameOrValue
  1. 如果 LoneUnicodePropertyNameOrValue 匹配的源文本Table 67 的 “Property name” 列中列出的字符串二元属性,则返回 true
  2. 返回 false
ClassUnion :: ClassSetRange ClassUnionopt
  1. 如果 ClassUnion 存在,则返回 ClassUnionMayContainStrings
  2. 返回 false
ClassUnion :: ClassSetOperand ClassUnionopt
  1. 如果 ClassSetOperandMayContainStringstrue,则返回 true
  2. 如果 ClassUnion 存在,则返回 ClassUnionMayContainStrings
  3. 返回 false
ClassIntersection :: ClassSetOperand && ClassSetOperand
  1. 如果第一个 ClassSetOperandMayContainStringsfalse,则返回 false
  2. 如果第二个 ClassSetOperandMayContainStringsfalse,则返回 false
  3. 返回 true
ClassIntersection :: ClassIntersection && ClassSetOperand
  1. 如果 ClassIntersectionMayContainStringsfalse,则返回 false
  2. 如果 ClassSetOperandMayContainStringsfalse,则返回 false
  3. 返回 true
ClassSubtraction :: ClassSetOperand -- ClassSetOperand
  1. 返回第一个 ClassSetOperandMayContainStrings
ClassSubtraction :: ClassSubtraction -- ClassSetOperand
  1. 返回 ClassSubtractionMayContainStrings
ClassStringDisjunctionContents :: ClassString | ClassStringDisjunctionContents
  1. 如果 ClassStringMayContainStringstrue,则返回 true
  2. 返回 ClassStringDisjunctionContentsMayContainStrings
ClassString :: [empty]
  1. 返回 true
ClassString :: NonEmptyClassString
  1. 返回 NonEmptyClassStringMayContainStrings
NonEmptyClassString :: ClassSetCharacter NonEmptyClassStringopt
  1. 如果 NonEmptyClassString 存在,则返回 true
  2. 返回 false

22.2.1.9 Static Semantics: GroupSpecifiersThatMatch ( thisGroupName )

The abstract operation GroupSpecifiersThatMatch takes argument thisGroupName (a GroupName Parse Node) and returns a List of GroupSpecifier Parse Nodes. It performs the following steps when called:

  1. namethisGroupNameCapturingGroupName
  2. pattern包含 thisGroupNamePattern
  3. result 为新的空 List
  4. pattern 包含的每个 GroupSpecifier groupSpecifier,执行:
    1. 如果 groupSpecifierCapturingGroupNamename,则
      1. groupSpecifier 追加到 result
  5. 返回 result

22.2.1.10 Static Semantics: CapturingGroupName

The syntax-directed operation CapturingGroupName takes no arguments and returns a String. It is defined piecewise over the following productions:

GroupName :: < RegExpIdentifierName >
  1. idTextUnescapedRegExpIdentifierNameRegExpIdentifierCodePoints
  2. 返回 CodePointsToString(idTextUnescaped)。

22.2.1.11 Static Semantics: RegExpIdentifierCodePoints

The syntax-directed operation RegExpIdentifierCodePoints takes no arguments and returns a List of code points. It is defined piecewise over the following productions:

RegExpIdentifierName :: RegExpIdentifierStart
  1. codePointRegExpIdentifierStartRegExpIdentifierCodePoint
  2. 返回 « codePoint »。
RegExpIdentifierName :: RegExpIdentifierName RegExpIdentifierPart
  1. codePoints 为派生的 RegExpIdentifierNameRegExpIdentifierCodePoints
  2. codePointRegExpIdentifierPartRegExpIdentifierCodePoint
  3. 返回 codePoints 和 « codePoint » 的列表连接

22.2.1.12 Static Semantics: RegExpIdentifierCodePoint

The syntax-directed operation RegExpIdentifierCodePoint takes no arguments and returns a code point. It is defined piecewise over the following productions:

RegExpIdentifierStart :: IdentifierStartChar
  1. 返回 IdentifierStartChar 匹配的码点。
RegExpIdentifierPart :: IdentifierPartChar
  1. 返回 IdentifierPartChar 匹配的码点。
RegExpIdentifierStart :: \ RegExpUnicodeEscapeSequence RegExpIdentifierPart :: \ RegExpUnicodeEscapeSequence
  1. 返回其数值为 RegExpUnicodeEscapeSequenceCharacterValue 的码点。
RegExpIdentifierStart :: UnicodeLeadSurrogate UnicodeTrailSurrogate RegExpIdentifierPart :: UnicodeLeadSurrogate UnicodeTrailSurrogate
  1. lead 为一个码元,其数值为 UnicodeLeadSurrogate 匹配的码点的数值。
  2. trail 为一个码元,其数值为 UnicodeTrailSurrogate 匹配的码点的数值。
  3. 返回 UTF16SurrogatePairToCodePoint(lead, trail)。

22.2.2 模式语义

正则表达式模式会使用下文描述的过程转换为 Abstract Closure。只要结果相同,鼓励实现使用比下列算法更高效的算法。该 Abstract Closure 用作 RegExp 对象的 [[RegExpMatcher]] 内部槽的值。

如果 Pattern 的相关标志既不包含 u 也不包含 v,则它是 BMP 模式。否则,它是 Unicode 模式。BMP 模式会匹配被解释为由 16 位值序列组成的 String,这些 16 位值是 Basic Multilingual Plane 范围内的 Unicode 码点。Unicode 模式会匹配被解释为由使用 UTF-16 编码的 Unicode 码点组成的 String。在描述 BMP 模式行为的上下文中,“字符”指单个 16 位 Unicode BMP 码点。在描述 Unicode 模式行为的上下文中,“字符”指 UTF-16 编码的码点(6.1.4)。在任一上下文中,“字符值”指对应的未编码码点的数值。

Pattern 的语法和语义被定义为好像 Pattern 的源文本是一个 SourceCharacter 值的 List,其中每个 SourceCharacter 对应一个 Unicode 码点。如果 BMP 模式包含非 BMP SourceCharacter,则整个模式会使用 UTF-16 编码,并使用该编码中的各个码元作为该 List 的元素。

Note

例如,考虑一个在源文本中表示为单个非 BMP 字符 U+1D11E(MUSICAL SYMBOL G CLEF)的模式。如果解释为 Unicode 模式,它将是一个由单个码点 U+1D11E 组成的单元素(字符)List。然而,如果解释为 BMP 模式,它会先被 UTF-16 编码,产生一个由码元 0xD834 和 0xDD1E 组成的双元素 List

模式作为 ECMAScript String 值传给 RegExp 构造器,其中非 BMP 字符以 UTF-16 编码。例如,单个字符 MUSICAL SYMBOL G CLEF 模式,表示为 String 值时,是一个长度为 2 的 String,其元素是码元 0xD834 和 0xDD1E。因此,将其处理为由两个模式字符组成的 BMP 模式时,不需要进一步转换该字符串。然而,将其处理为 Unicode 模式时,必须使用 UTF16SurrogatePairToCodePoint 来产生一个 List,该 List 的唯一元素是单个模式字符,即码点 U+1D11E。

实现实际上可以不执行这种到 UTF-16 或从 UTF-16 的转换,但本规范的语义要求模式匹配的结果必须如同执行了这种转换一样。

22.2.2.1 记法

下列描述使用以下内部数据结构:

  • CharSetElement 是以下两个实体之一:
    • 如果 regexpRecord.[[UnicodeSets]]false,则 CharSetElement 是上述 Pattern Semantics 意义上的一个字符。
    • 如果 regexpRecord.[[UnicodeSets]]true,则 CharSetElement 是一个序列,其元素是上述 Pattern Semantics 意义上的字符。这包括空序列、一个字符的序列以及多于一个字符的序列。为方便起见,在处理这种 CharSetElement 时,单个字符可与一个字符的序列互换对待。
  • CharSet 是 CharSetElement 的数学集合。
  • CaptureRangeRecord { [[StartIndex]], [[EndIndex]] },表示 capture 中包含的字符范围,其中 [[StartIndex]] 是表示该范围在 input 中开始索引(包含)的整数,[[EndIndex]] 是表示该范围在 input 中结束索引(不包含)的整数。对于任何 CaptureRange,这些索引都必须满足不变量 [[StartIndex]][[EndIndex]]
  • MatchStateRecord { [[Input]], [[EndIndex]], [[Captures]] },其中 [[Input]] 是表示被匹配 String 的字符 List[[EndIndex]] 是整数,[[Captures]] 是值的 List,对应模式中每个左捕获括号各一个。MatchState 用于在正则表达式匹配算法中表示部分匹配状态。[[EndIndex]] 是目前为止由模式匹配到的最后一个输入字符索引加一,而 [[Captures]] 保存捕获括号的结果。[[Captures]] 的第 nth 个元素要么是表示第 nth 组捕获括号所捕获字符范围的 CaptureRange,要么是 undefined,如果第 nth 组捕获括号尚未到达。由于回溯,在匹配过程中任意时刻都可能使用许多 MatchState
  • MatcherContinuation 是一个 Abstract Closure,它接受一个 MatchState 实参,并返回 MatchStatefailureMatcherContinuation 会尝试从其 MatchState 实参给定的中间状态开始,把模式剩余部分(由该 closure 的捕获值指定)与 input 进行匹配。如果匹配成功,MatcherContinuation 返回其到达的最终 MatchState;如果匹配失败,MatcherContinuation 返回 failure
  • Matcher 是一个 Abstract Closure,它接受两个实参——一个 MatchState 和一个 MatcherContinuation——并返回 MatchStatefailureMatcher 会尝试从其 MatchState 实参给定的中间状态开始,把模式的中间子模式(由该 closure 的捕获值指定)与 MatchState[[Input]] 进行匹配。MatcherContinuation 实参应是匹配模式其余部分的 closure。在匹配模式的子模式以获得新的 MatchState 后,Matcher 随后会在该新的 MatchState 上调用 MatcherContinuation,以测试模式的其余部分是否也能匹配。如果可以,Matcher 返回 MatcherContinuation 返回的 MatchState;如果不可以,Matcher 可以在其选择点尝试不同选择,反复调用 MatcherContinuation,直到成功或所有可能性都已耗尽。

22.2.2.1.1 RegExp Record

RegExp Record 是一种 Record 值,用于存储编译期间以及可能在匹配期间需要的 RegExp 相关信息。

它具有以下字段:

Table 64: RegExp Record 字段
字段名 含义
[[IgnoreCase]] a Boolean 指示 RegExp 的 flags 中是否出现 "i"
[[Multiline]] a Boolean 指示 RegExp 的 flags 中是否出现 "m"
[[DotAll]] a Boolean 指示 RegExp 的 flags 中是否出现 "s"
[[Unicode]] a Boolean 指示 RegExp 的 flags 中是否出现 "u"
[[UnicodeSets]] a Boolean 指示 RegExp 的 flags 中是否出现 "v"
[[CapturingGroupsCount]] a non-negative integer RegExp 模式中的左捕获括号数量

22.2.2.2 Runtime Semantics: CompilePattern

The syntax-directed operation CompilePattern takes argument regexpRecord (a RegExp Record) and returns an Abstract Closure that takes a List of characters and a non-negative integer and returns either a MatchState or failure. It is defined piecewise over the following productions:

Pattern :: Disjunction
  1. m 为以 regexpRecordforward 为实参执行 DisjunctionCompileSubpattern
  2. 返回一个新的 Abstract Closure,其参数为 (input, index),捕获 regexpRecordm,并在被调用时执行以下步骤:
    1. 断言:input 是字符 List
    2. 断言:0 ≤ indexinput 中的元素数量。
    3. c 为新的 MatcherContinuation,其参数为 (y),不捕获任何内容,并在被调用时执行以下步骤:
      1. 断言:yMatchState
      2. 返回 y
    4. capability 为由 regexpRecord.[[CapturingGroupsCount]]undefined 值组成的 List,索引从 1 到 regexpRecord.[[CapturingGroupsCount]]
    5. xMatchState { [[Input]]: input, [[EndIndex]]: index, [[Captures]]: capability }。
    6. 返回 m(x, c)。
Note

Pattern 会编译为 Abstract Closure 值。随后 RegExpBuiltinExec 可以把此过程应用于字符 List 以及该 List 中的偏移,以确定模式是否会从该 List 中的确切偏移开始匹配;如果确实匹配,则确定捕获括号的值是什么。22.2.2 中的算法设计为使编译模式可能抛出 SyntaxError 异常;另一方面,一旦模式成功编译,把所得 Abstract Closure 应用于在字符 List 中寻找匹配就不能抛出异常(除了可能在任何地方发生的任何由实现定义的异常,例如内存不足)。

22.2.2.3 Runtime Semantics: CompileSubpattern

The syntax-directed operation CompileSubpattern takes arguments regexpRecord (a RegExp Record) and direction (forward or backward) and returns a Matcher.

Note 1

本节在 B.1.2.5 中被修订。

It is defined piecewise over the following productions:

Disjunction :: Alternative | Disjunction
  1. m1 为以 regexpRecorddirection 为实参执行 AlternativeCompileSubpattern
  2. m2 为以 regexpRecorddirection 为实参执行 DisjunctionCompileSubpattern
  3. 返回 MatchTwoAlternatives(m1, m2)。
Note 2

| 正则表达式运算符分隔两个 alternatives。模式首先尝试匹配左侧 Alternative(后跟正则表达式的后续部分);如果失败,则尝试匹配右侧 Disjunction(后跟正则表达式的后续部分)。如果左侧 Alternative、右侧 Disjunction 和后续部分全都有选择点,则先尝试后续部分中的所有选择,然后再转到左侧 Alternative 中的下一个选择。如果左侧 Alternative 中的选择耗尽,则改为尝试右侧 Disjunction,而不是左侧 Alternative。由 | 跳过的模式部分中的任何捕获括号都会产生 undefined 值,而不是 Strings。因此,例如,

/a|ab/.exec("abc")

返回结果 "a",而不是 "ab"。此外,

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

返回数组

["abc", "a", "a", undefined, "bc", undefined, "bc"]

而不是

["abc", "ab", undefined, "ab", "c", "c", undefined]

尝试两个 alternatives 的顺序与 direction 的值无关。

Alternative :: [empty]
  1. 返回 EmptyMatcher()。
Alternative :: Alternative Term
  1. m1 为以 regexpRecorddirection 为实参执行 AlternativeCompileSubpattern
  2. m2 为以 regexpRecorddirection 为实参执行 TermCompileSubpattern
  3. 返回 MatchSequence(m1, m2, direction)。
Note 3

连续的 Term 会尝试同时匹配 input 的连续部分。当 directionforward 时,如果左侧 Alternative、右侧 Term 和正则表达式的后续部分全都有选择点,则先尝试后续部分中的所有选择,然后再转到右侧 Term 中的下一个选择;并且先尝试右侧 Term 中的所有选择,然后再转到左侧 Alternative 中的下一个选择。当 directionbackward 时,AlternativeTerm 的求值顺序会被反转。

Term :: Assertion
  1. 返回以 regexpRecord 为实参执行 AssertionCompileAssertion
Note 4

所得 Matcherdirection 无关。

Term :: Atom
  1. 返回以 regexpRecorddirection 为实参执行 AtomCompileAtom
Term :: Atom Quantifier
  1. m 为以 regexpRecorddirection 为实参执行 AtomCompileAtom
  2. qQuantifierCompileQuantifier
  3. 断言:q.[[Min]]q.[[Max]]
  4. parenIndexCountLeftCapturingParensBefore(Term)。
  5. parenCountCountLeftCapturingParensWithin(Atom)。
  6. 返回一个新的 Matcher,其参数为 (x, c),捕获 mqparenIndexparenCount,并在被调用时执行以下步骤:
    1. 断言:xMatchState
    2. 断言:cMatcherContinuation
    3. 返回 RepeatMatcher(m, q.[[Min]], q.[[Max]], q.[[Greedy]], x, c, parenIndex, parenCount)。

22.2.2.3.1 RepeatMatcher ( m, min, max, greedy, matchState, continue, 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), matchState (a MatchState), continue (a MatcherContinuation), parenIndex (a non-negative integer), and parenCount (a non-negative integer) and returns either a MatchState or failure. It performs the following steps when called:

  1. 如果 max = 0,则返回 continue(matchState)。
  2. d 为新的 MatcherContinuation,其参数为 (y),捕获 mminmaxgreedymatchStatecontinueparenIndexparenCount,并在被调用时执行以下步骤:
    1. 断言:yMatchState
    2. 如果 min = 0 且 y.[[EndIndex]] = matchState.[[EndIndex]],则返回 failure
    3. 如果 min = 0,则令 min2 为 0;否则令 min2min - 1。
    4. 如果 max = +∞,则令 max2 为 +∞;否则令 max2max - 1。
    5. 返回 RepeatMatcher(m, min2, max2, greedy, y, continue, parenIndex, parenCount)。
  3. capabilitymatchState.[[Captures]] 的副本。
  4. 对从 parenIndex + 1 到 parenIndex + parenCount闭区间内的每个整数 k,将 capability[k] 设置为 undefined
  5. inputmatchState.[[Input]]
  6. ematchState.[[EndIndex]]
  7. xrMatchState { [[Input]]: input, [[EndIndex]]: e, [[Captures]]: capability }。
  8. 如果 min ≠ 0,则返回 m(xr, d)。
  9. 如果 greedyfalse,则
    1. zcontinue(matchState)。
    2. 如果 z 不是 failure,则返回 z
    3. 返回 m(xr, d)。
  10. zm(xr, d)。
  11. 如果 z 不是 failure,则返回 z
  12. 返回 continue(matchState)。
Note 1

后跟 QuantifierAtom 会重复 Quantifier 指定的次数。Quantifier 可以是非贪婪的,在这种情况下 Atom 模式会以仍能匹配后续部分的最少次数重复;也可以是贪婪的,在这种情况下 Atom 模式会以仍能匹配后续部分的最多次数重复。被重复的是 Atom 模式,而不是它匹配到的输入字符序列,因此 Atom 的不同重复可以匹配不同的输入子字符串

Note 2

如果 Atom 和正则表达式的后续部分全都有选择点,则 Atom 首先会尽可能多次(或如果是非贪婪,则尽可能少次)匹配。先尝试后续部分中的所有选择,然后再转到 Atom 最后一次重复中的下一个选择。先尝试 Atom 最后一次(第 nth 次)重复中的所有选择,然后再转到 Atom 倒数第二次(第 n - 1st 次)重复中的下一个选择;此时可能会发现 Atom 现在可以进行更多或更少次重复;这些选择会被耗尽(同样,从尽可能少或尽可能多开始),然后再转到 Atom 第 (n - 1)st 次重复中的下一个选择,如此类推。

比较

/a[a-z]{2,4}/.exec("abcdefghi")

其返回 "abcde",以及

/a[a-z]{2,4}?/.exec("abcdefghi")

其返回 "abc"

还可考虑

/(aa|aabaac|ba|b|c)*/.exec("aabaac")

按照上述选择点顺序,它返回数组

["aaba", "ba"]

而不是以下任一:

["aabaac", "aabaac"]
["aabaac", "c"]

上述选择点顺序可用于编写一个计算两个数(以一元记法表示)最大公约数的正则表达式。以下示例计算 10 和 15 的 gcd:

"aaaaaaaaaa,aaaaaaaaaaaaaaa".replace(/^(a+)\1*,\1+$/, "$1")

其返回一元记法的 gcd "aaaaa"

Note 3

RepeatMatcher 的步骤 4 会在每次重复 Atom 时清除 Atom 的 captures。我们可以在正则表达式

/(z)((a+)?(b+)?(c))*/.exec("zaacbbbcac")

中看到它的行为,其返回数组

["zaacbbbcac", "z", "ac", "a", undefined, "c"]

而不是

["zaacbbbcac", "z", "ac", "a", "bbb", "c"]

因为最外层 * 的每次迭代都会清除被量化 Atom包含的所有捕获 Strings,在此情况下包括编号为 2、3、4 和 5 的捕获 Strings。

Note 4

RepeatMatcher 的步骤 2.b 规定,一旦已满足最小重复次数,就不再考虑对匹配空字符序列的 Atom 进行更多展开以进行进一步重复。这可以防止正则表达式引擎在如下模式上陷入无限循环:

/(a*)*/.exec("b")

或稍微更复杂的:

/(a*)b\1+/.exec("baaaac")

其返回数组

["b", ""]

22.2.2.3.2 EmptyMatcher ( )

The abstract operation EmptyMatcher takes no arguments and returns a Matcher. It performs the following steps when called:

  1. 返回一个新的 Matcher,其参数为 (matchState, continue),不捕获任何内容,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. 返回 continue(matchState)。

22.2.2.3.3 MatchTwoAlternatives ( m1, m2 )

The abstract operation MatchTwoAlternatives takes arguments m1 (a Matcher) and m2 (a Matcher) and returns a Matcher. It performs the following steps when called:

  1. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 m1m2,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. resultm1(matchState, continue)。
    4. 如果 result 不是 failure,则返回 result
    5. 返回 m2(matchState, continue)。

22.2.2.3.4 MatchSequence ( m1, m2, direction )

The abstract operation MatchSequence takes arguments m1 (a Matcher), m2 (a Matcher), and direction (forward or backward) and returns a Matcher. It performs the following steps when called:

  1. 如果 directionforward,则
    1. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 m1m2,并在被调用时执行以下步骤:
      1. 断言:matchStateMatchState
      2. 断言:continueMatcherContinuation
      3. d 为新的 MatcherContinuation,其参数为 (y),捕获 continuem2,并在被调用时执行以下步骤:
        1. 断言:yMatchState
        2. 返回 m2(y, continue)。
      4. 返回 m1(matchState, d)。
  2. 断言:directionbackward
  3. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 m1m2,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. d 为新的 MatcherContinuation,其参数为 (y),捕获 continuem1,并在被调用时执行以下步骤:
      1. 断言:yMatchState
      2. 返回 m1(y, continue)。
    4. 返回 m2(matchState, d)。

22.2.2.4 Runtime Semantics: CompileAssertion

The syntax-directed operation CompileAssertion takes argument regexpRecord (a RegExp Record) and returns a Matcher.

Note 1

本节在 B.1.2.6 中被修订。

It is defined piecewise over the following productions:

Assertion :: ^
  1. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 regexpRecord,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. inputmatchState.[[Input]]
    4. ematchState.[[EndIndex]]
    5. 如果 e = 0,或者如果 regexpRecord.[[Multiline]]true 且字符 input[e - 1] 被 LineTerminator 匹配,则
      1. 返回 continue(matchState)。
    6. 返回 failure
Note 2

即使 y 标志与模式一起使用,^ 也始终只匹配 input 开头,或(如果 regexpRecord.[[Multiline]]true)行开头。

Assertion :: $
  1. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 regexpRecord,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. inputmatchState.[[Input]]
    4. ematchState.[[EndIndex]]
    5. inputLengthinput 中的元素数量。
    6. 如果 e = inputLength,或者如果 regexpRecord.[[Multiline]]true 且字符 input[e] 被 LineTerminator 匹配,则
      1. 返回 continue(matchState)。
    7. 返回 failure
Assertion :: \b
  1. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 regexpRecord,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. inputmatchState.[[Input]]
    4. ematchState.[[EndIndex]]
    5. aIsWordChar(regexpRecord, input, e - 1)。
    6. bIsWordChar(regexpRecord, input, e)。
    7. 如果 atruebfalse,或者如果 afalsebtrue,则返回 continue(matchState)。
    8. 返回 failure
Assertion :: \B
  1. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 regexpRecord,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. inputmatchState.[[Input]]
    4. ematchState.[[EndIndex]]
    5. aIsWordChar(regexpRecord, input, e - 1)。
    6. bIsWordChar(regexpRecord, input, e)。
    7. 如果 atruebtrue,或者如果 afalsebfalse,则返回 continue(matchState)。
    8. 返回 failure
Assertion :: (?= Disjunction )
  1. m 为以 regexpRecordforward 为实参执行 DisjunctionCompileSubpattern
  2. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 m,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. d 为新的 MatcherContinuation,其参数为 (y),不捕获任何内容,并在被调用时执行以下步骤:
      1. 断言:yMatchState
      2. 返回 y
    4. resultm(matchState, d)。
    5. 如果 resultfailure,则返回 failure
    6. 断言:resultMatchState
    7. capabilityresult.[[Captures]]
    8. inputmatchState.[[Input]]
    9. xematchState.[[EndIndex]]
    10. zMatchState { [[Input]]: input, [[EndIndex]]: xe, [[Captures]]: capability }。
    11. 返回 continue(z)。
Note 3

形式 (?= Disjunction ) 指定零宽正向先行断言。为了使其成功,Disjunction 内的模式必须在当前位置匹配,但在匹配后续部分之前当前位置不会前进。如果 Disjunction 可以在当前位置以多种方式匹配,则只尝试第一种方式。与其他正则表达式运算符不同,不会回溯进入 (?= 形式(这种不寻常行为继承自 Perl)。只有当 Disjunction 包含捕获括号,并且模式的后续部分包含对这些 captures 的反向引用时,这一点才重要。

例如,

/(?=(a+))/.exec("baaabac")

匹配第一个 b 后紧接的空 String,因此返回数组:

["", "aaa"]

为说明不会回溯进入先行断言,请考虑:

/(?=(a+))a*b\1/.exec("baaabac")

此表达式返回

["aba", "a"]

而不是:

["aaaba", "a"]
Assertion :: (?! Disjunction )
  1. m 为以 regexpRecordforward 为实参执行 DisjunctionCompileSubpattern
  2. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 m,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. d 为新的 MatcherContinuation,其参数为 (y),不捕获任何内容,并在被调用时执行以下步骤:
      1. 断言:yMatchState
      2. 返回 y
    4. resultm(matchState, d)。
    5. 如果 result 不是 failure,则返回 failure
    6. 返回 continue(matchState)。
Note 4

形式 (?! Disjunction ) 指定零宽负向先行断言。为了使其成功,Disjunction 内的模式必须无法在当前位置匹配。在匹配后续部分之前当前位置不会前进。Disjunction 可以包含捕获括号,但对它们的反向引用只有在 Disjunction 自身内部才有意义。从模式其他地方对这些捕获括号进行的反向引用始终返回 undefined,因为负向先行断言必须失败,整个模式才能成功。例如,

/(.*?)a(?!(a+)b\2c)\2(.*)/.exec("baaabaac")

会寻找一个 a,其后不紧跟某个正数 n 个 a、一个 b、另一个 n 个 a(由第一个 \2 指定)以及一个 c。第二个 \2 位于负向先行断言之外,因此它与 undefined 匹配并因此总是成功。整个表达式返回数组:

["baaabaac", "ba", undefined, "abaac"]
Assertion :: (?<= Disjunction )
  1. m 为以 regexpRecordbackward 为实参执行 DisjunctionCompileSubpattern
  2. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 m,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. d 为新的 MatcherContinuation,其参数为 (y),不捕获任何内容,并在被调用时执行以下步骤:
      1. 断言:yMatchState
      2. 返回 y
    4. resultm(matchState, d)。
    5. 如果 resultfailure,则返回 failure
    6. 断言:resultMatchState
    7. capabilityresult.[[Captures]]
    8. inputmatchState.[[Input]]
    9. xematchState.[[EndIndex]]
    10. zMatchState { [[Input]]: input, [[EndIndex]]: xe, [[Captures]]: capability }。
    11. 返回 continue(z)。
Assertion :: (?<! Disjunction )
  1. m 为以 regexpRecordbackward 为实参执行 DisjunctionCompileSubpattern
  2. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 m,并在被调用时执行以下步骤:
    1. 断言:matchStateMatchState
    2. 断言:continueMatcherContinuation
    3. d 为新的 MatcherContinuation,其参数为 (y),不捕获任何内容,并在被调用时执行以下步骤:
      1. 断言:yMatchState
      2. 返回 y
    4. resultm(matchState, d)。
    5. 如果 result 不是 failure,则返回 failure
    6. 返回 continue(matchState)。

22.2.2.4.1 IsWordChar ( regexpRecord, input, e )

The abstract operation IsWordChar takes arguments regexpRecord (a RegExp Record), input (a List of characters), and e (an integer) and returns a Boolean. It performs the following steps when called:

  1. inputLengthinput 中的元素数量。
  2. 如果 e = -1 或 e = inputLength,则返回 false
  3. char 为字符 input[e]。
  4. 如果 WordCharacters(regexpRecord) 包含 char,则返回 true
  5. 返回 false

22.2.2.5 Runtime Semantics: CompileQuantifier

The syntax-directed operation CompileQuantifier takes no arguments and returns a Record with fields [[Min]] (a non-negative integer), [[Max]] (a non-negative integer or +∞), and [[Greedy]] (a Boolean). It is defined piecewise over the following productions:

Quantifier :: QuantifierPrefix
  1. qpQuantifierPrefixCompileQuantifierPrefix
  2. 返回 Record { [[Min]]: qp.[[Min]], [[Max]]: qp.[[Max]], [[Greedy]]: true }。
Quantifier :: QuantifierPrefix ?
  1. qpQuantifierPrefixCompileQuantifierPrefix
  2. 返回 Record { [[Min]]: qp.[[Min]], [[Max]]: qp.[[Max]], [[Greedy]]: false }。

22.2.2.6 Runtime Semantics: CompileQuantifierPrefix

The syntax-directed operation CompileQuantifierPrefix takes no arguments and returns a Record with fields [[Min]] (a non-negative integer) and [[Max]] (a non-negative integer or +∞). It is defined piecewise over the following productions:

QuantifierPrefix :: *
  1. 返回 Record { [[Min]]: 0, [[Max]]: +∞ }。
QuantifierPrefix :: +
  1. 返回 Record { [[Min]]: 1, [[Max]]: +∞ }。
QuantifierPrefix :: ?
  1. 返回 Record { [[Min]]: 0, [[Max]]: 1 }。
QuantifierPrefix :: { DecimalDigits }
  1. iDecimalDigits 的 MV(见 12.9.3)。
  2. 返回 Record { [[Min]]: i, [[Max]]: i }。
QuantifierPrefix :: { DecimalDigits ,}
  1. iDecimalDigits 的 MV。
  2. 返回 Record { [[Min]]: i, [[Max]]: +∞ }。
QuantifierPrefix :: { DecimalDigits , DecimalDigits }
  1. i 为第一个 DecimalDigits 的 MV。
  2. j 为第二个 DecimalDigits 的 MV。
  3. 返回 Record { [[Min]]: i, [[Max]]: j }。

22.2.2.7 Runtime Semantics: CompileAtom

The syntax-directed operation CompileAtom takes arguments regexpRecord (a RegExp Record) and direction (forward or backward) and returns a Matcher.

Note 1

本节在 B.1.2.7 中被修订。

It is defined piecewise over the following productions:

Atom :: PatternCharacter
  1. charPatternCharacter 匹配的字符。
  2. charSet包含字符 char 的单元素 CharSet
  3. 返回 CharacterSetMatcher(regexpRecord, charSet, false, direction)。
Atom :: .
  1. charSetAllCharacters(regexpRecord)。
  2. 如果 regexpRecord.[[DotAll]] 不是 true,则
    1. charSet 中移除所有与 LineTerminator 产生式右侧码点对应的字符。
  3. 返回 CharacterSetMatcher(regexpRecord, charSet, false, direction)。
Atom :: CharacterClass
  1. cc 为以 regexpRecord 为实参执行 CharacterClassCompileCharacterClass
  2. cscc.[[CharSet]]
  3. 如果 regexpRecord.[[UnicodeSets]]false,或 cs 的每个 CharSetElement 都由单个字符组成(包括 cs 为空的情况),则返回 CharacterSetMatcher(regexpRecord, cs, cc.[[Invert]], direction)。
  4. 断言:cc.[[Invert]]false
  5. listOfMatchers 为空 Matcher List
  6. cs包含多于 1 个字符的每个 CharSetElement s,按长度降序迭代,执行:
    1. cs2包含 s 最后一个码点的单元素 CharSet
    2. m2CharacterSetMatcher(regexpRecord, cs2, false, direction)。
    3. s 中每个码点 c1,从其倒数第二个码点开始向后迭代,执行:
      1. cs1包含 c1 的单元素 CharSet
      2. m1CharacterSetMatcher(regexpRecord, cs1, false, direction)。
      3. m2 设置为 MatchSequence(m1, m2, direction)。
    4. m2 追加到 listOfMatchers
  7. singles包含 cs 中所有由单个字符组成的 CharSetElementCharSet
  8. CharacterSetMatcher(regexpRecord, singles, false, direction) 追加到 listOfMatchers
  9. 如果 cs 包含空字符序列,则将 EmptyMatcher() 追加到 listOfMatchers
  10. m2listOfMatchers 中最后一个 Matcher
  11. listOfMatchers 的每个 Matcher m1,从倒数第二个元素开始向后迭代,执行:
    1. m2 设置为 MatchTwoAlternatives(m1, m2)。
  12. 返回 m2
Atom :: ( GroupSpecifieropt Disjunction )
  1. m 为以 regexpRecorddirection 为实参执行 DisjunctionCompileSubpattern
  2. parenIndexCountLeftCapturingParensBefore(Atom)。
  3. 返回一个新的 Matcher,其参数为 (x, c),捕获 directionmparenIndex,并在被调用时执行以下步骤:
    1. 断言:xMatchState
    2. 断言:cMatcherContinuation
    3. d 为新的 MatcherContinuation,其参数为 (y),捕获 xcdirectionparenIndex,并在被调用时执行以下步骤:
      1. 断言:yMatchState
      2. capabilityy.[[Captures]] 的副本。
      3. inputx.[[Input]]
      4. xex.[[EndIndex]]
      5. yey.[[EndIndex]]
      6. 如果 directionforward,则
        1. 断言:xeye
        2. rCaptureRange { [[StartIndex]]: xe, [[EndIndex]]: ye }。
      7. 否则,
        1. 断言:directionbackward
        2. 断言:yexe
        3. rCaptureRange { [[StartIndex]]: ye, [[EndIndex]]: xe }。
      8. capability[parenIndex + 1] 设置为 r
      9. zMatchState { [[Input]]: input, [[EndIndex]]: ye, [[Captures]]: capability }。
      10. 返回 c(z)。
    4. 返回 m(x, d)。
Note 2

形式为 ( Disjunction ) 的括号既用于把 Disjunction 模式的组成部分组合在一起,也用于保存匹配结果。该结果可以用于反向引用(\ 后跟非零十进制数)、在替换 String 中被引用,或作为从正则表达式匹配 Abstract Closure 返回的数组的一部分。要抑制括号的捕获行为,请改用形式 (?: Disjunction )

Atom :: (? RegularExpressionModifiers : Disjunction )
  1. addModifiersRegularExpressionModifiers 匹配的源文本
  2. removeModifiers 为空 String。
  3. modifiedRerUpdateModifiers(regexpRecord, CodePointsToString(addModifiers), removeModifiers)。
  4. 返回以 modifiedRerdirection 为实参执行 DisjunctionCompileSubpattern
Atom :: (? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction )
  1. addModifiers 为第一个 RegularExpressionModifiers 匹配的源文本
  2. removeModifiers 为第二个 RegularExpressionModifiers 匹配的源文本
  3. modifiedRerUpdateModifiers(regexpRecord, CodePointsToString(addModifiers), CodePointsToString(removeModifiers))。
  4. 返回以 modifiedRerdirection 为实参执行 DisjunctionCompileSubpattern
AtomEscape :: DecimalEscape
  1. nDecimalEscapeCapturingGroupNumber
  2. 断言:nregexpRecord.[[CapturingGroupsCount]]
  3. 返回 BackreferenceMatcher(regexpRecord, « n », direction)。
Note 3

形式为 \ 后跟非零十进制数 n 的转义序列,会匹配第 nth 组捕获括号的结果(22.2.2.1)。如果正则表达式的捕获括号少于 n 个,则为错误。如果正则表达式有 n 个或更多捕获括号,但第 nth 个由于尚未捕获任何内容而为 undefined,则该反向引用始终成功。

AtomEscape :: CharacterEscape
  1. charValueCharacterEscapeCharacterValue
  2. char 为其字符值为 charValue 的字符。
  3. charSet包含字符 char 的单元素 CharSet
  4. 返回 CharacterSetMatcher(regexpRecord, charSet, false, direction)。
AtomEscape :: CharacterClassEscape
  1. cs 为以 regexpRecord 为实参执行 CharacterClassEscapeCompileToCharSet
  2. 如果 regexpRecord.[[UnicodeSets]]false,或 cs 的每个 CharSetElement 都由单个字符组成(包括 cs 为空的情况),则返回 CharacterSetMatcher(regexpRecord, cs, false, direction)。
  3. listOfMatchers 为空 Matcher List
  4. cs包含多于 1 个字符的每个 CharSetElement s,按长度降序迭代,执行:
    1. cs2包含 s 最后一个码点的单元素 CharSet
    2. m2CharacterSetMatcher(regexpRecord, cs2, false, direction)。
    3. s 中每个码点 c1,从其倒数第二个码点开始向后迭代,执行:
      1. cs1包含 c1 的单元素 CharSet
      2. m1CharacterSetMatcher(regexpRecord, cs1, false, direction)。
      3. m2 设置为 MatchSequence(m1, m2, direction)。
    4. m2 追加到 listOfMatchers
  5. singles包含 cs 中所有由单个字符组成的 CharSetElementCharSet
  6. CharacterSetMatcher(regexpRecord, singles, false, direction) 追加到 listOfMatchers
  7. 如果 cs 包含空字符序列,则将 EmptyMatcher() 追加到 listOfMatchers
  8. m2listOfMatchers 中最后一个 Matcher
  9. listOfMatchers 的每个 Matcher m1,从倒数第二个元素开始向后迭代,执行:
    1. m2 设置为 MatchTwoAlternatives(m1, m2)。
  10. 返回 m2
AtomEscape :: k GroupName
  1. matchingGroupSpecifiersGroupSpecifiersThatMatch(GroupName)。
  2. parenIndices 为新的空 List
  3. matchingGroupSpecifiers 的每个 GroupSpecifier groupSpecifier,执行:
    1. parenIndexCountLeftCapturingParensBefore(groupSpecifier)。
    2. parenIndex 追加到 parenIndices
  4. 返回 BackreferenceMatcher(regexpRecord, parenIndices, direction)。

22.2.2.7.1 CharacterSetMatcher ( regexpRecord, charSet, invert, direction )

The abstract operation CharacterSetMatcher takes arguments regexpRecord (a RegExp Record), charSet (a CharSet), invert (a Boolean), and direction (forward or backward) and returns a Matcher. It performs the following steps when called:

  1. 如果 regexpRecord.[[UnicodeSets]]true,则
    1. 断言:invertfalse
    2. 断言:charSet 的每个 CharSetElement 都由单个字符组成。
  2. 返回一个新的 Matcher,其参数为 (x, c),捕获 regexpRecordcharSetinvertdirection,并在被调用时执行以下步骤:
    1. 断言:xMatchState
    2. 断言:cMatcherContinuation
    3. inputx.[[Input]]
    4. endIndexx.[[EndIndex]]
    5. 如果 directionforward,则令 fendIndex + 1。
    6. 否则,令 fendIndex - 1。
    7. inputLengthinput 中的元素数量。
    8. 如果 f < 0 或 f > inputLength,则返回 failure
    9. indexmin(endIndex, f)。
    10. char 为字符 input[index]。
    11. ccCanonicalize(regexpRecord, char)。
    12. 如果 charSet 中存在一个 CharSetElement,它恰好包含一个字符 a,并且 Canonicalize(regexpRecord, a) 是 cc,则令 foundtrue;否则令 foundfalse
    13. 如果 invertfalsefoundfalse,则返回 failure
    14. 如果 inverttruefoundtrue,则返回 failure
    15. capabilityx.[[Captures]]
    16. yMatchState { [[Input]]: input, [[EndIndex]]: f, [[Captures]]: capability }。
    17. 返回 c(y)。

22.2.2.7.2 BackreferenceMatcher ( regexpRecord, ns, direction )

The abstract operation BackreferenceMatcher takes arguments regexpRecord (a RegExp Record), ns (a List of positive integers), and direction (forward or backward) and returns a Matcher. It performs the following steps when called:

  1. 返回一个新的 Matcher,其参数为 (x, c),捕获 regexpRecordnsdirection,并在被调用时执行以下步骤:
    1. 断言:xMatchState
    2. 断言:cMatcherContinuation
    3. inputx.[[Input]]
    4. capabilityx.[[Captures]]
    5. rundefined
    6. ns 的每个整数 n,执行:
      1. 如果 capability[n] 不是 undefined,则
        1. 断言:rundefined
        2. r 设置为 capability[n]。
    7. 如果 rundefined,则返回 c(x)。
    8. endIndexx.[[EndIndex]]
    9. rsr.[[StartIndex]]
    10. rer.[[EndIndex]]
    11. lengthre - rs
    12. 如果 directionforward,则令 fendIndex + length
    13. 否则,令 fendIndex - length
    14. inputLengthinput 中的元素数量。
    15. 如果 f < 0 或 f > inputLength,则返回 failure
    16. gmin(endIndex, f)。
    17. 如果存在从 0(包含)到 length(不包含区间内的整数 i,使得 Canonicalize(regexpRecord, input[rs + i]) 不是 Canonicalize(regexpRecord, input[g + i]),则返回 failure
    18. yMatchState { [[Input]]: input, [[EndIndex]]: f, [[Captures]]: capability }。
    19. 返回 c(y)。

22.2.2.7.3 Canonicalize ( regexpRecord, char )

The abstract operation Canonicalize takes arguments regexpRecord (a RegExp Record) and char (a character) and returns a character. It performs the following steps when called:

  1. 如果 HasEitherUnicodeFlag(regexpRecord) 是 trueregexpRecord.[[IgnoreCase]]true,则
    1. 如果 Unicode Character Database 的文件 CaseFolding.txtchar 提供 simple 或 common case folding 映射,则返回把该映射应用于 char 的结果。
    2. 返回 char
  2. 如果 regexpRecord.[[IgnoreCase]]false,则返回 char
  3. 断言:char 是 UTF-16 码元。
  4. codePoint 为其数值为 char 的数值的码点。
  5. u 为依据 Unicode Default Case Conversion 算法的 toUppercase(« codePoint »)。
  6. uStringCodePointsToString(u)。
  7. 如果 uString 的长度 ≠ 1,则返回 char
  8. codeUnituString 的单个码元元素。
  9. 如果 char 的数值 ≥ 128 且 codeUnit 的数值 < 128,则返回 char
  10. 返回 codeUnit
Note

HasEitherUnicodeFlag(regexpRecord) 是 true 的大小写无关匹配中,所有字符在比较之前都会隐式使用 Unicode Standard 提供的 simple 映射进行 case-fold。simple 映射始终映射到单个码点,因此它不会例如把 ß(U+00DF LATIN SMALL LETTER SHARP S)映射为 ssSS。不过,它可能把 Basic Latin 块之外的码点映射到其中的码点——例如,ſ(U+017F LATIN SMALL LETTER LONG S)会 case-fold 为 s(U+0073 LATIN SMALL LETTER S),而 (U+212A KELVIN SIGN)会 case-fold 为 k(U+006B LATIN SMALL LETTER K)。包含这些码点的 Strings 会被诸如 /[a-z]/ui 的正则表达式匹配。

HasEitherUnicodeFlag(regexpRecord) 是 false 的大小写无关匹配中,映射基于 Unicode Default Case Conversion 算法 toUppercase,而不是 toCasefold,这会导致一些细微差异。例如,(U+2126 OHM SIGN)会被 toUppercase 映射为其自身,但会被 toCasefold 映射为 ω(U+03C9 GREEK SMALL LETTER OMEGA),同时 Ω(U+03A9 GREEK CAPITAL LETTER OMEGA)也是如此,因此 "\u2126" 会被 /[ω]/ui/[\u03A9]/ui 匹配,但不会被 /[ω]/i/[\u03A9]/i 匹配。此外,Basic Latin 块之外的码点不会被映射到其中的码点,因此诸如 "\u017F ſ""\u212A K" 的字符串不会被 /[a-z]/i 匹配。

22.2.2.7.4 UpdateModifiers ( regexpRecord, add, remove )

The abstract operation UpdateModifiers takes arguments regexpRecord (a RegExp Record), add (a String), and remove (a String) and returns a RegExp Record. It performs the following steps when called:

  1. 断言:addremove 没有共同元素。
  2. ignoreCaseregexpRecord.[[IgnoreCase]]
  3. multilineregexpRecord.[[Multiline]]
  4. dotAllregexpRecord.[[DotAll]]
  5. unicoderegexpRecord.[[Unicode]]
  6. unicodeSetsregexpRecord.[[UnicodeSets]]
  7. capturingGroupsCountregexpRecord.[[CapturingGroupsCount]]
  8. 如果 remove 包含 "i",则将 ignoreCase 设置为 false
  9. 否则,如果 add 包含 "i",则将 ignoreCase 设置为 true
  10. 如果 remove 包含 "m",则将 multiline 设置为 false
  11. 否则,如果 add 包含 "m",则将 multiline 设置为 true
  12. 如果 remove 包含 "s",则将 dotAll 设置为 false
  13. 否则,如果 add 包含 "s",则将 dotAll 设置为 true
  14. 返回 RegExp Record { [[IgnoreCase]]: ignoreCase, [[Multiline]]: multiline, [[DotAll]]: dotAll, [[Unicode]]: unicode, [[UnicodeSets]]: unicodeSets, [[CapturingGroupsCount]]: capturingGroupsCount }。

22.2.2.8 Runtime Semantics: CompileCharacterClass

The syntax-directed operation CompileCharacterClass takes argument regexpRecord (a RegExp Record) and returns a Record with fields [[CharSet]] (a CharSet) and [[Invert]] (a Boolean). It is defined piecewise over the following productions:

CharacterClass :: [ ClassContents ]
  1. charSet 为以 regexpRecord 为实参执行 ClassContentsCompileToCharSet
  2. 返回 Record { [[CharSet]]: charSet, [[Invert]]: false }。
CharacterClass :: [^ ClassContents ]
  1. charSet 为以 regexpRecord 为实参执行 ClassContentsCompileToCharSet
  2. 如果 regexpRecord.[[UnicodeSets]]true,则
    1. 返回 Record { [[CharSet]]: CharacterComplement(regexpRecord, charSet), [[Invert]]: false }。
  3. 返回 Record { [[CharSet]]: charSet, [[Invert]]: true }。

22.2.2.9 Runtime Semantics: CompileToCharSet

The syntax-directed operation CompileToCharSet takes argument regexpRecord (a RegExp Record) and returns a CharSet.

Note 1

本节在 B.1.2.8 中被修订。

It is defined piecewise over the following productions:

ClassContents :: [empty]
  1. 返回空 CharSet
NonemptyClassRanges :: ClassAtom NonemptyClassRangesNoDash
  1. charSet 为以 regexpRecord 为实参执行 ClassAtomCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参执行 NonemptyClassRangesNoDashCompileToCharSet
  3. 返回 CharSet charSetotherSet 的并集。
NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents
  1. charSet 为以 regexpRecord 为实参执行第一个 ClassAtomCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参执行第二个 ClassAtomCompileToCharSet
  3. remainingSet 为以 regexpRecord 为实参执行 ClassContentsCompileToCharSet
  4. rangeSetCharacterRange(charSet, otherSet)。
  5. 返回 rangeSetremainingSet 的并集。
NonemptyClassRangesNoDash :: ClassAtomNoDash NonemptyClassRangesNoDash
  1. charSet 为以 regexpRecord 为实参执行 ClassAtomNoDashCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参执行 NonemptyClassRangesNoDashCompileToCharSet
  3. 返回 CharSet charSetotherSet 的并集。
NonemptyClassRangesNoDash :: ClassAtomNoDash - ClassAtom ClassContents
  1. charSet 为以 regexpRecord 为实参执行 ClassAtomNoDashCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参执行 ClassAtomCompileToCharSet
  3. remainingSet 为以 regexpRecord 为实参执行 ClassContentsCompileToCharSet
  4. rangeSetCharacterRange(charSet, otherSet)。
  5. 返回 rangeSetremainingSet 的并集。
Note 2

ClassContents 可以展开为单个 ClassAtom,和/或由短横线分隔的两个 ClassAtom 的范围。在后一种情况下,ClassContents 包括第一个 ClassAtom 与第二个 ClassAtom 之间的所有字符,包含两端;如果任一 ClassAtom 不表示单个字符(例如,如果其中一个是 \w),或者如果第一个 ClassAtom 的字符值严格大于第二个 ClassAtom 的字符值,则会发生错误。

Note 3

即使模式忽略大小写,范围两端的大小写对于确定哪些字符属于该范围也是重要的。因此,例如,模式 /[E-F]/i 只匹配字母 EFef,而模式 /[E-f]/i 会匹配 Unicode Basic Latin 块中的所有大写和小写字母,以及符号 [, \, ], ^, _`

Note 4

- 字符可以被按字面处理,也可以表示一个范围。如果它是 ClassContents 的第一个或最后一个字符、某个范围指定的开始或结束界限,或者紧随范围指定之后,则按字面处理。

ClassAtom :: -
  1. 返回包含单个字符 - U+002D(HYPHEN-MINUS)的 CharSet
ClassAtomNoDash :: SourceCharacter but not one of \ or ] or -
  1. 返回包含 SourceCharacter 匹配的字符的 CharSet
ClassEscape :: b - CharacterEscape
  1. charValue 为此 ClassEscapeCharacterValue
  2. char 为其字符值为 charValue 的字符。
  3. 返回包含单个字符 charCharSet
Note 5

ClassAtom 可以使用正则表达式其余部分中允许的任意转义序列,但 \b\B 和反向引用除外。在 CharacterClass 内部,\b 表示退格字符,而 \B 和反向引用会引发错误。在 ClassAtom 内使用反向引用会导致错误。

CharacterClassEscape :: d
  1. 返回包含字符 0123456789 的十元素 CharSet
CharacterClassEscape :: D
  1. charSet CharacterClassEscape :: d 返回的 CharSet
  2. 返回 CharacterComplement(regexpRecord, charSet)。
CharacterClassEscape :: s
  1. 返回包含WhiteSpaceLineTerminator 产生式右侧码点对应的所有字符的 CharSet
CharacterClassEscape :: S
  1. charSet CharacterClassEscape :: s 返回的 CharSet
  2. 返回 CharacterComplement(regexpRecord, charSet)。
CharacterClassEscape :: w
  1. 返回 MaybeSimpleCaseFolding(regexpRecord, WordCharacters(regexpRecord))。
CharacterClassEscape :: W
  1. charSet CharacterClassEscape :: w 返回的 CharSet
  2. 返回 CharacterComplement(regexpRecord, charSet)。
CharacterClassEscape :: p{ UnicodePropertyValueExpression }
  1. 返回以 regexpRecord 为实参执行 UnicodePropertyValueExpressionCompileToCharSet
CharacterClassEscape :: P{ UnicodePropertyValueExpression }
  1. charSet 为以 regexpRecord 为实参执行 UnicodePropertyValueExpressionCompileToCharSet
  2. 断言:charSet包含单个码点。
  3. 返回 CharacterComplement(regexpRecord, charSet)。
UnicodePropertyValueExpression :: UnicodePropertyName = UnicodePropertyValue
  1. psUnicodePropertyName 匹配的源文本
  2. pUnicodeMatchProperty(regexpRecord, ps)。
  3. 断言:pTable 65 的 “Property name and aliases” 列中列出的 Unicode 属性名或属性别名。
  4. vsUnicodePropertyValue 匹配的源文本
  5. vUnicodeMatchPropertyValue(p, vs)。
  6. charSet包含所有 Unicode 码点的 CharSet,这些码点的字符数据库定义包含值为 v 的属性 p
  7. 返回 MaybeSimpleCaseFolding(regexpRecord, charSet)。
UnicodePropertyValueExpression :: LoneUnicodePropertyNameOrValue
  1. sLoneUnicodePropertyNameOrValue 匹配的源文本
  2. 如果 UnicodeMatchPropertyValue(General_Category, s) 是 PropertyValueAliases.txt 中列出的 General_Category (gc) 属性的 Unicode 属性值或属性值别名,则
    1. 返回包含所有 Unicode 码点的 CharSet,这些码点的字符数据库定义包含值为 s 的属性 “General_Category”。
  3. pUnicodeMatchProperty(regexpRecord, s)。
  4. 断言:pTable 66 的 “Property name and aliases” 列中列出的二元 Unicode 属性或二元属性别名,或是 Table 67 的 “Property name” 列中列出的字符串二元 Unicode 属性。
  5. charSet包含所有 CharSetElementCharSet,这些元素的字符数据库定义包含值为 “True” 的属性 p
  6. 返回 MaybeSimpleCaseFolding(regexpRecord, charSet)。
ClassUnion :: ClassSetRange ClassUnionopt
  1. charSet 为以 regexpRecord 为实参执行 ClassSetRangeCompileToCharSet
  2. 如果 ClassUnion 存在,则
    1. otherSet 为以 regexpRecord 为实参执行 ClassUnionCompileToCharSet
    2. 返回 CharSet charSetotherSet 的并集。
  3. 返回 charSet
ClassUnion :: ClassSetOperand ClassUnionopt
  1. charSet 为以 regexpRecord 为实参执行 ClassSetOperandCompileToCharSet
  2. 如果 ClassUnion 存在,则
    1. otherSet 为以 regexpRecord 为实参执行 ClassUnionCompileToCharSet
    2. 返回 CharSet charSetotherSet 的并集。
  3. 返回 charSet
ClassIntersection :: ClassSetOperand && ClassSetOperand
  1. charSet 为以 regexpRecord 为实参执行第一个 ClassSetOperandCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参执行第二个 ClassSetOperandCompileToCharSet
  3. 返回 CharSet charSetotherSet 的交集。
ClassIntersection :: ClassIntersection && ClassSetOperand
  1. charSet 为以 regexpRecord 为实参执行 ClassIntersectionCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参执行 ClassSetOperandCompileToCharSet
  3. 返回 CharSet charSetotherSet 的交集。
ClassSubtraction :: ClassSetOperand -- ClassSetOperand
  1. charSet 为以 regexpRecord 为实参执行第一个 ClassSetOperandCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参执行第二个 ClassSetOperandCompileToCharSet
  3. 返回包含 charSet 中不是 otherSetCharSetElement 的那些 CharSetElementCharSet
ClassSubtraction :: ClassSubtraction -- ClassSetOperand
  1. charSet 为以 regexpRecord 为实参执行 ClassSubtractionCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参执行 ClassSetOperandCompileToCharSet
  3. 返回包含 charSet 中不是 otherSetCharSetElement 的那些 CharSetElementCharSet
ClassSetRange :: ClassSetCharacter - ClassSetCharacter
  1. charSet 为以 regexpRecord 为实参执行第一个 ClassSetCharacterCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参执行第二个 ClassSetCharacterCompileToCharSet
  3. 返回 MaybeSimpleCaseFolding(regexpRecord, CharacterRange(charSet, otherSet))。
Note 6

结果通常会由两个或更多范围组成。当 UnicodeSets 是 true 且 IgnoreCase 是 true 时,MaybeSimpleCaseFolding(regexpRecord, [Ā-č]) 将只包含该范围内奇数编号的码点。

ClassSetOperand :: ClassSetCharacter
  1. charSet 为以 regexpRecord 为实参执行 ClassSetCharacterCompileToCharSet
  2. 返回 MaybeSimpleCaseFolding(regexpRecord, charSet)。
ClassSetOperand :: ClassStringDisjunction
  1. charSet 为以 regexpRecord 为实参执行 ClassStringDisjunctionCompileToCharSet
  2. 返回 MaybeSimpleCaseFolding(regexpRecord, charSet)。
ClassSetOperand :: NestedClass
  1. 返回以 regexpRecord 为实参执行 NestedClassCompileToCharSet
NestedClass :: [ ClassContents ]
  1. 返回以 regexpRecord 为实参执行 ClassContentsCompileToCharSet
NestedClass :: [^ ClassContents ]
  1. charSet 为以 regexpRecord 为实参执行 ClassContentsCompileToCharSet
  2. 返回 CharacterComplement(regexpRecord, charSet)。
NestedClass :: \ CharacterClassEscape
  1. 返回以 regexpRecord 为实参执行 CharacterClassEscapeCompileToCharSet
ClassStringDisjunction :: \q{ ClassStringDisjunctionContents }
  1. 返回以 regexpRecord 为实参执行 ClassStringDisjunctionContentsCompileToCharSet
ClassStringDisjunctionContents :: ClassString
  1. s 为以 regexpRecord 为实参执行 ClassStringCompileClassSetString
  2. 返回包含一个字符串 sCharSet
ClassStringDisjunctionContents :: ClassString | ClassStringDisjunctionContents
  1. s 为以 regexpRecord 为实参执行 ClassStringCompileClassSetString
  2. charSet包含一个字符串 sCharSet
  3. otherSet 为以 regexpRecord 为实参执行 ClassStringDisjunctionContentsCompileToCharSet
  4. 返回 CharSet charSetotherSet 的并集。
ClassSetCharacter :: SourceCharacter but not ClassSetSyntaxCharacter \ CharacterEscape \ ClassSetReservedPunctuator
  1. charValue 为此 ClassSetCharacterCharacterValue
  2. char 为其字符值为 charValue 的字符。
  3. 返回包含单个字符 charCharSet
ClassSetCharacter :: \b
  1. 返回包含单个字符 U+0008(BACKSPACE)的 CharSet

22.2.2.9.1 CharacterRange ( charSet, otherSet )

The abstract operation CharacterRange takes arguments charSet (a CharSet) and otherSet (a CharSet) and returns a CharSet. It performs the following steps when called:

  1. 断言:charSetotherSet 都恰好包含一个字符。
  2. aCharSet charSet 中的一个字符。
  3. bCharSet otherSet 中的一个字符。
  4. i 为字符 a 的字符值。
  5. j 为字符 b 的字符值。
  6. 断言:ij
  7. 返回包含所有字符的 CharSet,这些字符的字符值位于从 ij闭区间内。

22.2.2.9.2 HasEitherUnicodeFlag ( regexpRecord )

The abstract operation HasEitherUnicodeFlag takes argument regexpRecord (a RegExp Record) and returns a Boolean. It performs the following steps when called:

  1. 如果 regexpRecord.[[Unicode]]trueregexpRecord.[[UnicodeSets]]true,则返回 true
  2. 返回 false

22.2.2.9.3 WordCharacters ( regexpRecord )

The abstract operation WordCharacters takes argument regexpRecord (a RegExp Record) and returns a CharSet. 返回一个 CharSet,其中包含\b\B\w\W 的目的而被视为“word characters”的字符。 It performs the following steps when called:

  1. basicWordChars包含 ASCII word characters 中每个字符的 CharSet
  2. extraWordChars包含所有字符 cCharSet,使得 c 不在 basicWordChars 中但 Canonicalize(regexpRecord, c) 在 basicWordChars 中。
  3. 断言:除非 HasEitherUnicodeFlag(regexpRecord) 是 trueregexpRecord.[[IgnoreCase]]true,否则 extraWordChars 为空。
  4. 返回 basicWordCharsextraWordChars 的并集。

22.2.2.9.4 AllCharacters ( regexpRecord )

The abstract operation AllCharacters takes argument regexpRecord (a RegExp Record) and returns a CharSet. 根据正则表达式标志返回“所有字符”的集合。 It performs the following steps when called:

  1. 如果 regexpRecord.[[UnicodeSets]]trueregexpRecord.[[IgnoreCase]]true,则
    1. 返回包含所有没有 Simple Case Folding 映射(即 scf(c)=c)的 Unicode 码点 cCharSet
  2. 如果 HasEitherUnicodeFlag(regexpRecord) 是 true,则
    1. 返回包含所有码点值的 CharSet
  3. 返回包含所有码元值的 CharSet

22.2.2.9.5 MaybeSimpleCaseFolding ( regexpRecord, charSet )

The abstract operation MaybeSimpleCaseFolding takes arguments regexpRecord (a RegExp Record) and charSet (a CharSet) and returns a CharSet. 如果 regexpRecord.[[UnicodeSets]]falseregexpRecord.[[IgnoreCase]]false,则返回 charSet。否则,它使用 Unicode Character Database 文件 CaseFolding.txt 中的 Simple Case Foldingscf(codePoint))定义(每个定义都会把单个码点映射到另一个单个码点),逐字符地把 charSet 的每个 CharSetElement 映射到规范形式,并返回所得 CharSet。 It performs the following steps when called:

  1. 如果 regexpRecord.[[UnicodeSets]]falseregexpRecord.[[IgnoreCase]]false,则返回 charSet
  2. otherSet 为新的空 CharSet
  3. charSet 的每个 CharSetElement s,执行:
    1. t 为空字符序列。
    2. s 中的每个单一码点 codePoint,执行:
      1. scf(codePoint) 追加到 t
    3. t 添加到 otherSet
  4. 返回 otherSet

22.2.2.9.6 CharacterComplement ( regexpRecord, complement )

The abstract operation CharacterComplement takes arguments regexpRecord (a RegExp Record) and complement (a CharSet) and returns a CharSet. It performs the following steps when called:

  1. charSetAllCharacters(regexpRecord)。
  2. 返回包含 charSet 中不是 complementCharSetElement 的那些 CharSetElementCharSet

22.2.2.9.7 UnicodeMatchProperty ( regexpRecord, p )

The abstract operation UnicodeMatchProperty takes arguments regexpRecord (a RegExp Record) and p (ECMAScript source text) and returns a Unicode property name. It performs the following steps when called:

  1. 如果 regexpRecord.[[UnicodeSets]]truepTable 67 的 “Property name” 列中列出的 Unicode 属性名,则
    1. 返回 Unicode 码点 List p
  2. 断言:pTable 65Table 66 的 “Property name and aliases” 列中列出的 Unicode 属性名或属性别名。
  3. c 为对应行的 “Canonical property name” 列中给出的 p 的规范 property name
  4. 返回 Unicode 码点 List c

实现必须支持 Table 65Table 66Table 67 中列出的 Unicode property names 和别名。为确保互操作性,实现不得支持任何其他 property names 或别名。

Note 1

例如,Script_Extensionsproperty name)和 scx(属性别名)是有效的,但 script_extensionsScx 不是。

Note 2

所列属性形成了 UTS18 RL1.2 所要求内容的超集。

Note 3

这些表中条目的拼写(包括大小写)与 Unicode Character Database 文件 PropertyAliases.txt 中使用的拼写一致。该文件中的精确拼写保证稳定

Table 65: Non-binary Unicode property aliases and their canonical property names
Property name and aliases Canonical property name
General_Category General_Category
gc
Script Script
sc
Script_Extensions Script_Extensions
scx
Table 66: Binary Unicode property aliases and their canonical property names
Property name and aliases Canonical property name
ASCII ASCII
ASCII_Hex_Digit ASCII_Hex_Digit
AHex
Alphabetic Alphabetic
Alpha
Any Any
Assigned Assigned
Bidi_Control Bidi_Control
Bidi_C
Bidi_Mirrored Bidi_Mirrored
Bidi_M
Case_Ignorable Case_Ignorable
CI
Cased Cased
Changes_When_Casefolded Changes_When_Casefolded
CWCF
Changes_When_Casemapped Changes_When_Casemapped
CWCM
Changes_When_Lowercased Changes_When_Lowercased
CWL
Changes_When_NFKC_Casefolded Changes_When_NFKC_Casefolded
CWKCF
Changes_When_Titlecased Changes_When_Titlecased
CWT
Changes_When_Uppercased Changes_When_Uppercased
CWU
Dash Dash
Default_Ignorable_Code_Point Default_Ignorable_Code_Point
DI
Deprecated Deprecated
Dep
Diacritic Diacritic
Dia
Emoji Emoji
Emoji_Component Emoji_Component
EComp
Emoji_Modifier Emoji_Modifier
EMod
Emoji_Modifier_Base Emoji_Modifier_Base
EBase
Emoji_Presentation Emoji_Presentation
EPres
Extended_Pictographic Extended_Pictographic
ExtPict
Extender Extender
Ext
Grapheme_Base Grapheme_Base
Gr_Base
Grapheme_Extend Grapheme_Extend
Gr_Ext
Hex_Digit Hex_Digit
Hex
IDS_Binary_Operator IDS_Binary_Operator
IDSB
IDS_Trinary_Operator IDS_Trinary_Operator
IDST
ID_Continue ID_Continue
IDC
ID_Start ID_Start
IDS
Ideographic Ideographic
Ideo
Join_Control Join_Control
Join_C
Logical_Order_Exception Logical_Order_Exception
LOE
Lowercase Lowercase
Lower
Math Math
Noncharacter_Code_Point Noncharacter_Code_Point
NChar
Pattern_Syntax Pattern_Syntax
Pat_Syn
Pattern_White_Space Pattern_White_Space
Pat_WS
Quotation_Mark Quotation_Mark
QMark
Radical Radical
Regional_Indicator Regional_Indicator
RI
Sentence_Terminal Sentence_Terminal
STerm
Soft_Dotted Soft_Dotted
SD
Terminal_Punctuation Terminal_Punctuation
Term
Unified_Ideograph Unified_Ideograph
UIdeo
Uppercase Uppercase
Upper
Variation_Selector Variation_Selector
VS
White_Space White_Space
space
XID_Continue XID_Continue
XIDC
XID_Start XID_Start
XIDS
Table 67: Binary Unicode properties of strings
Property name
Basic_Emoji
Emoji_Keycap_Sequence
RGI_Emoji_Modifier_Sequence
RGI_Emoji_Flag_Sequence
RGI_Emoji_Tag_Sequence
RGI_Emoji_ZWJ_Sequence
RGI_Emoji

22.2.2.9.8 UnicodeMatchPropertyValue ( p, v )

The abstract operation UnicodeMatchPropertyValue takes arguments p (ECMAScript source text) and v (ECMAScript source text) and returns a Unicode property value. It performs the following steps when called:

  1. 断言:pTable 65 的 “Canonical property name” 列中列出的规范、非别名 Unicode property name
  2. 断言:vPropertyValueAliases.txt 中列出的 Unicode 属性 p 的属性值或属性值别名。
  3. value 为对应行的 “Canonical property value” 列中给出的 v 的规范属性值。
  4. 返回 Unicode 码点 List value

实现必须支持 PropertyValueAliases.txt 中针对 Table 65 所列属性列出的 Unicode 属性值和属性值别名。为确保互操作性,实现不得支持任何其他属性值或属性值别名。

Note 1

例如,XpeoOld_Persian 是有效的 Script_Extensions 值,但 xpeoOld Persian 不是。

Note 2

此算法不同于 UAX44 中列出的符号值匹配规则:大小写、空白、U+002D(HYPHEN-MINUS)和 U+005F(LOW LINE)不会被忽略,并且不支持 Is 前缀。

22.2.2.10 Runtime Semantics: CompileClassSetString

The syntax-directed operation CompileClassSetString takes argument regexpRecord (a RegExp Record) and returns a sequence of characters. It is defined piecewise over the following productions:

ClassString :: [empty]
  1. 返回空字符序列。
ClassString :: NonEmptyClassString
  1. 返回以 regexpRecord 为实参执行 NonEmptyClassStringCompileClassSetString
NonEmptyClassString :: ClassSetCharacter NonEmptyClassStringopt
  1. cs 为以 regexpRecord 为实参执行 ClassSetCharacterCompileToCharSet
  2. s1 为字符序列,它是 cs 中的单个 CharSetElement
  3. 如果 NonEmptyClassString 存在,则
    1. s2 为以 regexpRecord 为实参执行 NonEmptyClassStringCompileClassSetString
    2. 返回 s1s2 的连接。
  4. 返回 s1

22.2.3 RegExp 创建的抽象操作

22.2.3.1 RegExpCreate ( pattern, flags )

The abstract operation RegExpCreate takes arguments pattern (an ECMAScript language value) and flags (a String or undefined) and returns either a normal completion containing an Object or a throw completion. It performs the following steps when called:

  1. obj 为 ! RegExpAlloc(%RegExp%)。
  2. 返回 ? RegExpInitialize(obj, pattern, flags)。

22.2.3.2 RegExpAlloc ( newTarget )

The abstract operation RegExpAlloc takes argument newTarget (a constructor) and returns either a normal completion containing an Object or a throw completion. It performs the following steps when called:

  1. obj 为 ? OrdinaryCreateFromConstructor(newTarget, "%RegExp.prototype%", « [[OriginalSource]], [[OriginalFlags]], [[RegExpRecord]], [[RegExpMatcher]] »)。
  2. 执行 ! DefinePropertyOrThrow(obj, "lastIndex", PropertyDescriptor { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false })。
  3. 返回 obj

22.2.3.3 RegExpInitialize ( obj, pattern, flags )

The abstract operation RegExpInitialize takes arguments obj (an Object), pattern (an ECMAScript language value), and flags (an ECMAScript language value) and returns either a normal completion containing an Object or a throw completion. It performs the following steps when called:

  1. 如果 patternundefined,则将 pattern 设置为空 String。
  2. 否则,将 pattern 设置为 ? ToString(pattern)。
  3. 如果 flagsundefined,则将 flags 设置为空 String。
  4. 否则,将 flags 设置为 ? ToString(flags)。
  5. 如果 flags 包含"d""g""i""m""s""u""v""y" 之外的任何码元,则抛出 SyntaxError 异常。
  6. 如果 flags 包含任何码元超过一次,则抛出 SyntaxError 异常。
  7. 如果 flags 包含 "i",则令 itrue;否则令 ifalse
  8. 如果 flags 包含 "m",则令 mtrue;否则令 mfalse
  9. 如果 flags 包含 "s",则令 strue;否则令 sfalse
  10. 如果 flags 包含 "u",则令 utrue;否则令 ufalse
  11. 如果 flags 包含 "v",则令 vtrue;否则令 vfalse
  12. 如果 utruevtrue,则
    1. patternTextStringToCodePoints(pattern)。
  13. 否则,
    1. patternText 为将 pattern 的每个 16 位元素解释为 Unicode BMP 码点的结果。不会对这些元素应用 UTF-16 解码。
  14. parseResultParsePattern(patternText, u, v)。
  15. 如果 parseResult 是非空的 SyntaxError 对象 List,则抛出 SyntaxError 异常。
  16. 断言:parseResultPattern Parse Node
  17. obj.[[OriginalSource]] 设置为 pattern
  18. obj.[[OriginalFlags]] 设置为 flags
  19. capturingGroupsCountCountLeftCapturingParensWithin(parseResult)。
  20. regexpRecordRegExp Record { [[IgnoreCase]]: i, [[Multiline]]: m, [[DotAll]]: s, [[Unicode]]: u, [[UnicodeSets]]: v, [[CapturingGroupsCount]]: capturingGroupsCount }。
  21. obj.[[RegExpRecord]] 设置为 regexpRecord
  22. obj.[[RegExpMatcher]] 设置为以 regexpRecord 为实参执行 parseResultCompilePattern
  23. 执行 ? Set(obj, "lastIndex", +0𝔽, true)。
  24. 返回 obj

22.2.3.4 Static Semantics: ParsePattern ( patternText, u, v )

The abstract operation ParsePattern takes arguments patternText (a sequence of Unicode code points), u (a Boolean), and v (a Boolean) and returns a Parse Node or a non-empty List of SyntaxError objects.

Note

本节在 B.1.2.9 中被修订。

It performs the following steps when called:

  1. 如果 vtrueutrue,则
    1. parseResult包含一个或多个 SyntaxError 对象的 List
  2. 否则,如果 vtrue,则
    1. parseResultParseText(patternText, Pattern[+UnicodeMode, +UnicodeSetsMode, +NamedCaptureGroups])。
  3. 否则,如果 utrue,则
    1. parseResultParseText(patternText, Pattern[+UnicodeMode, ~UnicodeSetsMode, +NamedCaptureGroups])。
  4. 否则,
    1. parseResultParseText(patternText, Pattern[~UnicodeMode, ~UnicodeSetsMode, +NamedCaptureGroups])。
  5. 返回 parseResult

22.2.4 RegExp 构造器

RegExp 构造器:

  • %RegExp%
  • 全局对象"RegExp" 属性的初始值。
  • 在作为构造器调用时创建并初始化一个新的 RegExp 对象。
  • 在作为函数而非构造器调用时,返回一个新的 RegExp 对象,或者如果唯一实参是 RegExp 对象,则返回该实参自身。
  • 可以用作类定义的 extends 子句的值。意图继承指定 RegExp 行为的子类构造器必须包含对 RegExp 构造器的 super 调用,以创建并初始化具有必要内部槽的子类实例。

22.2.4.1 RegExp ( patternOrRegexp, flags )

此函数在被调用时执行以下步骤:

  1. patternIsRegExp 为 ? IsRegExp(patternOrRegexp)。
  2. 如果 NewTarget 是 undefined,则
    1. newTarget活动函数对象
    2. 如果 patternIsRegExptrueflagsundefined,则
      1. patternCtor 为 ? Get(patternOrRegexp, "constructor")。
      2. 如果 SameValue(newTarget, patternCtor) 是 true,则返回 patternOrRegexp
  3. 否则,
    1. newTarget 为 NewTarget。
  4. 如果 patternOrRegexp 是 Object 且 patternOrRegexp 具有 [[RegExpMatcher]] 内部槽,则
    1. patternSourcepatternOrRegexp.[[OriginalSource]]
    2. 如果 flagsundefined,则将 flags 设置为 patternOrRegexp.[[OriginalFlags]]
  5. 否则,如果 patternIsRegExptrue,则
    1. patternSource 为 ? Get(patternOrRegexp, "source")。
    2. 如果 flagsundefined,则
      1. flags 设置为 ? Get(patternOrRegexp, "flags")。
  6. 否则,
    1. patternSourcepatternOrRegexp
  7. obj 为 ? RegExpAlloc(newTarget)。
  8. 返回 ? RegExpInitialize(obj, patternSource, flags)。
Note

如果 pattern 使用 StringLiteral 提供,则通常的转义序列替换会在该 String 被此函数处理之前执行。如果 pattern 必须包含一个转义序列才能被此函数识别,则任何 U+005C(REVERSE SOLIDUS)码点都必须在 StringLiteral 内被转义,以防止它们在形成 StringLiteral 的内容时被移除。

22.2.5 RegExp 构造器的属性

RegExp 构造器:

22.2.5.1 RegExp.escape ( string )

此函数返回 string 的副本,其中正则表达式 Pattern 中可能具有特殊含义的字符已被替换为等价的转义序列。

它在被调用时执行以下步骤:

  1. 如果 string 不是 String,则抛出 TypeError 异常。
  2. escaped 为空 String。
  3. codePointListStringToCodePoints(string)。
  4. codePointList 的每个码点 codePoint,执行:
    1. 如果 escaped 为空 String 且 codePointDecimalDigitAsciiLetter 匹配,则
      1. 注:转义开头的数字可确保输出对应于可在 \0 字符转义或诸如 \1DecimalEscape 之后使用的模式文本,并且仍匹配 string,而不是被解释为前一个转义序列的扩展。转义开头的 ASCII 字母对 \c 之后的上下文也有同样作用。
      2. numericValuecodePoint 的数值。
      3. hexNumber::toString(𝔽(numericValue), 16)。
      4. 断言:hex 的长度为 2。
      5. escaped 设置为码元 0x005C(REVERSE SOLIDUS)、"x"hex字符串连接
    2. 否则,
      1. escaped 设置为 escapedEncodeForRegExpEscape(codePoint) 的字符串连接
  5. 返回 escaped
Note

尽管名称相似,EscapeRegExpPatternRegExp.escape 并不执行相似的操作。前者转义模式以便表示为字符串,而此函数转义字符串以便表示在模式内部。

22.2.5.1.1 EncodeForRegExpEscape ( codePoint )

The abstract operation EncodeForRegExpEscape takes argument codePoint (a code point) and returns a String. 它返回一个表示 Pattern 的 String,用于匹配 codePoint。如果 codePoint 是空白或 ASCII 标点符号,则返回值是转义序列。否则,返回值是 codePoint 自身的 String 表示。 It performs the following steps when called:

  1. 如果 codePointSyntaxCharacter 匹配,或 codePoint 是 U+002F(SOLIDUS),则
    1. 返回 0x005C(REVERSE SOLIDUS)和 UTF16EncodeCodePoint(codePoint) 的字符串连接
  2. 如果 codePointTable 63 的 “Code Point” 列中列出的码点,则
    1. 返回 0x005C(REVERSE SOLIDUS)和其 “Code Point” 列包含 codePoint 的行的 “ControlEscape” 列中的字符串的字符串连接
  3. otherPunctuators",-=<>#&!%:;@~'`" 和码元 0x0022(QUOTATION MARK)的字符串连接
  4. toEscapeStringToCodePoints(otherPunctuators)。
  5. 如果 toEscape 包含 codePointcodePointWhiteSpaceLineTerminator 匹配,或 codePoint 与前导代理或尾随代理具有相同数值,则
    1. codePointNumbercodePoint 的数值。
    2. 如果 codePointNumber ≤ 0xFF,则
      1. hexNumber::toString(𝔽(codePointNumber), 16)。
      2. 返回码元 0x005C(REVERSE SOLIDUS)、"x"StringPad(hex, 2, "0", start) 的字符串连接
    3. escaped 为空 String。
    4. codeUnitsUTF16EncodeCodePoint(codePoint)。
    5. codeUnits 的每个码元 codeUnit,执行:
      1. escaped 设置为 escapedUnicodeEscape(codeUnit) 的字符串连接
    6. 返回 escaped
  6. 返回 UTF16EncodeCodePoint(codePoint)。

22.2.5.2 RegExp.prototype

RegExp.prototype 的初始值是 RegExp 原型对象

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

22.2.5.3 get RegExp [ %Symbol.species% ]

RegExp[%Symbol.species%]访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. 返回 this 值。

此函数的 "name" 属性的值是 "get [Symbol.species]"

Note

RegExp 原型方法通常使用其 this 值的构造器来创建派生对象。然而,子类构造器可以通过重新定义其 %Symbol.species% 属性来覆盖这种默认行为。

22.2.6 RegExp 原型对象的属性

RegExp 原型对象

  • %RegExp.prototype%
  • 普通对象
  • 不是 RegExp 实例,并且没有 [[RegExpMatcher]] 内部槽,也没有 RegExp 实例对象的任何其他内部槽。
  • 具有一个 [[Prototype]] 内部槽,其值为 %Object.prototype%
Note

RegExp 原型对象自身没有 "valueOf" 属性;不过,它从 Object 原型对象继承 "valueOf" 属性。

22.2.6.1 RegExp.prototype.constructor

RegExp.prototype.constructor 的初始值是 %RegExp%

22.2.6.2 RegExp.prototype.exec ( string )

此方法在 string 中搜索正则表达式模式的出现位置,并返回包含匹配结果的 Array;如果 string 未匹配,则返回 null

它在被调用时执行以下步骤:

  1. regexpthis 值。
  2. 执行 ? RequireInternalSlot(regexp, [[RegExpMatcher]])。
  3. string 设置为 ? ToString(string)。
  4. 返回 ? RegExpBuiltinExec(regexp, string)。

22.2.6.3 get RegExp.prototype.dotAll

RegExp.prototype.dotAll访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. regexpthis 值。
  2. codeUnit 为码元 0x0073(LATIN SMALL LETTER S)。
  3. 返回 ? RegExpHasFlag(regexp, codeUnit)。

22.2.6.4 get RegExp.prototype.flags

RegExp.prototype.flags访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. regexpthis 值。
  2. 如果 regexp 不是 Object,则抛出 TypeError 异常。
  3. codeUnits 为新的空 List
  4. hasIndicesToBoolean(? Get(regexp, "hasIndices")).
  5. 如果 hasIndicestrue,则将码元 0x0064(LATIN SMALL LETTER D)追加到 codeUnits
  6. globalToBoolean(? Get(regexp, "global")).
  7. 如果 globaltrue,则将码元 0x0067(LATIN SMALL LETTER G)追加到 codeUnits
  8. ignoreCaseToBoolean(? Get(regexp, "ignoreCase")).
  9. 如果 ignoreCasetrue,则将码元 0x0069(LATIN SMALL LETTER I)追加到 codeUnits
  10. multilineToBoolean(? Get(regexp, "multiline")).
  11. 如果 multilinetrue,则将码元 0x006D(LATIN SMALL LETTER M)追加到 codeUnits
  12. dotAllToBoolean(? Get(regexp, "dotAll")).
  13. 如果 dotAlltrue,则将码元 0x0073(LATIN SMALL LETTER S)追加到 codeUnits
  14. unicodeToBoolean(? Get(regexp, "unicode")).
  15. 如果 unicodetrue,则将码元 0x0075(LATIN SMALL LETTER U)追加到 codeUnits
  16. unicodeSetsToBoolean(? Get(regexp, "unicodeSets")).
  17. 如果 unicodeSetstrue,则将码元 0x0076(LATIN SMALL LETTER V)追加到 codeUnits
  18. stickyToBoolean(? Get(regexp, "sticky")).
  19. 如果 stickytrue,则将码元 0x0079(LATIN SMALL LETTER Y)追加到 codeUnits
  20. 返回一个 String 值,其码元是 List codeUnits 的元素。如果 codeUnits 没有元素,则返回空 String。

22.2.6.4.1 RegExpHasFlag ( regexp, codeUnit )

The abstract operation RegExpHasFlag takes arguments regexp (an ECMAScript language value) and codeUnit (a code unit) and returns either a normal completion containing either a Boolean or undefined, or a throw completion. It performs the following steps when called:

  1. 如果 regexp 不是 Object,则抛出 TypeError 异常。
  2. 如果 regexp 没有 [[OriginalFlags]] 内部槽,则
    1. 如果 SameValue(regexp, %RegExp.prototype%) 是 true,则返回 undefined
    2. 抛出 TypeError 异常。
  3. flagsregexp.[[OriginalFlags]]
  4. 如果 flags 包含 codeUnit,则返回 true
  5. 返回 false

22.2.6.5 get RegExp.prototype.global

RegExp.prototype.global访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. regexpthis 值。
  2. codeUnit 为码元 0x0067(LATIN SMALL LETTER G)。
  3. 返回 ? RegExpHasFlag(regexp, codeUnit)。

22.2.6.6 get RegExp.prototype.hasIndices

RegExp.prototype.hasIndices访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. regexpthis 值。
  2. codeUnit 为码元 0x0064(LATIN SMALL LETTER D)。
  3. 返回 ? RegExpHasFlag(regexp, codeUnit)。

22.2.6.7 get RegExp.prototype.ignoreCase

RegExp.prototype.ignoreCase访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. regexpthis 值。
  2. codeUnit 为码元 0x0069(LATIN SMALL LETTER I)。
  3. 返回 ? RegExpHasFlag(regexp, codeUnit)。

22.2.6.8 RegExp.prototype [ %Symbol.match% ] ( string )

此方法在被调用时执行以下步骤:

  1. regexpthis 值。
  2. 如果 regexp 不是 Object,则抛出 TypeError 异常。
  3. string 设置为 ? ToString(string)。
  4. flags 为 ? ToString(? Get(regexp, "flags"))。
  5. 如果 flags包含 "g",则返回 ? RegExpExec(regexp, string)。
  6. 如果 flags 包含 "u"flags 包含 "v",则令 fullUnicodetrue;否则令 fullUnicodefalse
  7. 执行 ? Set(regexp, "lastIndex", +0𝔽, true)。
  8. array 为 ! ArrayCreate(0)。
  9. matchCount 为 0。
  10. 重复,
    1. result 为 ? RegExpExec(regexp, string)。
    2. 如果 resultnull,则
      1. 如果 matchCount = 0,则返回 null
      2. 返回 array
    3. matchString 为 ? ToString(? Get(result, "0"))。
    4. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(matchCount)), matchString)。
    5. 如果 matchString 为空 String,则
      1. thisIndex(? ToLength(? Get(regexp, "lastIndex"))).
      2. nextIndexAdvanceStringIndex(string, thisIndex, fullUnicode)。
      3. 执行 ? Set(regexp, "lastIndex", 𝔽(nextIndex), true)。
    6. matchCount 设置为 matchCount + 1。

此方法的 "name" 属性的值是 "[Symbol.match]"

Note

%Symbol.match% 属性由 IsRegExp 抽象操作用来标识具有正则表达式基本行为的对象。缺少 %Symbol.match% 属性,或存在这样的属性但其值经 Boolean 强制转换后不是 true,表示该对象并不意图作为正则表达式对象使用。

22.2.6.9 RegExp.prototype [ %Symbol.matchAll% ] ( string )

此方法在被调用时执行以下步骤:

  1. regexpthis 值。
  2. 如果 regexp 不是 Object,则抛出 TypeError 异常。
  3. string 设置为 ? ToString(string)。
  4. speciesCtor 为 ? SpeciesConstructor(regexp, %RegExp%)。
  5. flags 为 ? ToString(? Get(regexp, "flags"))。
  6. matcher 为 ? Construct(speciesCtor, « regexp, flags »)。
  7. lastIndex 为 ? ToLength(? Get(regexp, "lastIndex")).
  8. 执行 ? Set(matcher, "lastIndex", lastIndex, true)。
  9. 如果 flags 包含 "g",则令 globaltrue
  10. 否则,令 globalfalse
  11. 如果 flags 包含 "u"flags 包含 "v",则令 fullUnicodetrue
  12. 否则,令 fullUnicodefalse
  13. 返回 CreateRegExpStringIterator(matcher, string, global, fullUnicode)。

此方法的 "name" 属性的值是 "[Symbol.matchAll]"

22.2.6.10 get RegExp.prototype.multiline

RegExp.prototype.multiline访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. regexpthis 值。
  2. codeUnit 为码元 0x006D(LATIN SMALL LETTER M)。
  3. 返回 ? RegExpHasFlag(regexp, codeUnit)。

22.2.6.11 RegExp.prototype [ %Symbol.replace% ] ( string, replaceValue )

此方法在被调用时执行以下步骤:

  1. regexpthis 值。
  2. 如果 regexp 不是 Object,则抛出 TypeError 异常。
  3. string 设置为 ? ToString(string)。
  4. stringLengthstring 的长度。
  5. functionalReplaceIsCallable(replaceValue)。
  6. 如果 functionalReplacefalse,则
    1. replaceValue 设置为 ? ToString(replaceValue)。
  7. flags 为 ? ToString(? Get(regexp, "flags"))。
  8. 如果 flags 包含 "g",则令 globaltrue;否则令 globalfalse
  9. 如果 globaltrue,则
    1. 执行 ? Set(regexp, "lastIndex", +0𝔽, true)。
  10. results 为新的空 List
  11. donefalse
  12. 重复,只要 donefalse
    1. result 为 ? RegExpExec(regexp, string)。
    2. 如果 resultnull,则
      1. done 设置为 true
    3. 否则,
      1. result 追加到 results
      2. 如果 globalfalse,则
        1. done 设置为 true
      3. 否则,
        1. matchString 为 ? ToString(? Get(result, "0"))。
        2. 如果 matchString 为空 String,则
          1. thisIndex(? ToLength(? Get(regexp, "lastIndex"))).
          2. 如果 flags 包含 "u"flags 包含 "v",则令 fullUnicodetrue;否则令 fullUnicodefalse
          3. nextIndexAdvanceStringIndex(string, thisIndex, fullUnicode)。
          4. 执行 ? Set(regexp, "lastIndex", 𝔽(nextIndex), true)。
  13. accumulatedResult 为空 String。
  14. nextSourcePosition 为 0。
  15. results 的每个元素 result,执行:
    1. resultLength 为 ? LengthOfArrayLike(result)。
    2. capturesCountmax(resultLength - 1, 0)。
    3. matched 为 ? ToString(? Get(result, "0"))。
    4. matchLengthmatched 的长度。
    5. position 为 ? ToIntegerOrInfinity(? Get(result, "index"))。
    6. position 设置为将 position 夹在 0 与 stringLength 之间的结果。
    7. captures 为新的空 List
    8. captureNumber 为 1。
    9. 重复,只要 captureNumbercapturesCount
      1. capture 为 ? Get(result, ! ToString(𝔽(captureNumber)))。
      2. 如果 capture 不是 undefined,则
        1. capture 设置为 ? ToString(capture)。
      3. capture 追加到 captures
      4. 注:当 captureNumber = 1 时,前一步把第一个元素放入 captures(索引为 0)。更一般地,第 captureNumberth 个 capture(第 captureNumberth 组捕获括号捕获的字符)位于 captures[captureNumber - 1]。
      5. captureNumber 设置为 captureNumber + 1。
    10. namedCaptures 为 ? Get(result, "groups")。
    11. 如果 functionalReplacetrue,则
      1. replacerArgs 为 « matched »、captures 和 « 𝔽(position), string » 的列表连接
      2. 如果 namedCaptures 不是 undefined,则
        1. namedCaptures 追加到 replacerArgs
      3. replacementValue 为 ? Call(replaceValue, undefined, replacerArgs)。
      4. replacementString 为 ? ToString(replacementValue)。
    12. 否则,
      1. 如果 namedCaptures 不是 undefined,则
        1. namedCaptures 设置为 ? ToObject(namedCaptures)。
      2. replacementString 为 ? GetSubstitution(matched, string, position, captures, namedCaptures, replaceValue)。
    13. 如果 positionnextSourcePosition,则
      1. 注:position 通常不应向后移动。如果它向后移动,则表明 RegExp 子类行为不良,或使用访问触发的副作用改变了 regexp 的 global 标志或其他特征。在这些情况下,对应的替换会被忽略。
      2. accumulatedResult 设置为 accumulatedResultstringnextSourcePositionposition子字符串,以及 replacementString字符串连接
      3. nextSourcePosition 设置为 position + matchLength
  16. 如果 nextSourcePositionstringLength,则返回 accumulatedResult
  17. 返回 accumulatedResultstringnextSourcePosition 起的子字符串字符串连接

此方法的 "name" 属性的值是 "[Symbol.replace]"

22.2.6.12 RegExp.prototype [ %Symbol.search% ] ( string )

此方法在被调用时执行以下步骤:

  1. regexpthis 值。
  2. 如果 regexp 不是 Object,则抛出 TypeError 异常。
  3. string 设置为 ? ToString(string)。
  4. previousLastIndex 为 ? Get(regexp, "lastIndex")。
  5. 如果 previousLastIndex 不是 +0𝔽,则
    1. 执行 ? Set(regexp, "lastIndex", +0𝔽, true)。
  6. result 为 ? RegExpExec(regexp, string)。
  7. currentLastIndex 为 ? Get(regexp, "lastIndex")。
  8. 如果 SameValue(currentLastIndex, previousLastIndex) 是 false,则
    1. 执行 ? Set(regexp, "lastIndex", previousLastIndex, true)。
  9. 如果 resultnull,则返回 -1𝔽
  10. 返回 ? Get(result, "index")。

此方法的 "name" 属性的值是 "[Symbol.search]"

Note

执行搜索时会忽略此 RegExp 对象的 "lastIndex""global" 属性。"lastIndex" 属性保持不变。

22.2.6.13 get RegExp.prototype.source

RegExp.prototype.source访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. regexpthis 值。
  2. 如果 regexp 不是 Object,则抛出 TypeError 异常。
  3. 如果 regexp 没有 [[OriginalSource]] 内部槽,则
    1. 如果 SameValue(regexp, %RegExp.prototype%) 是 true,则返回 "(?:)"
    2. 抛出 TypeError 异常。
  4. 断言:regexp 具有 [[OriginalFlags]] 内部槽。
  5. sourceregexp.[[OriginalSource]]
  6. flagsregexp.[[OriginalFlags]]
  7. 返回 EscapeRegExpPattern(source, flags)。

22.2.6.13.1 EscapeRegExpPattern ( pattern, flags )

The abstract operation EscapeRegExpPattern takes arguments pattern (a String) and flags (a String) and returns a String. It performs the following steps when called:

  1. 如果 flags 包含 "v",则
    1. patternSymbolPattern[+UnicodeMode, +UnicodeSetsMode]
  2. 否则,如果 flags 包含 "u",则
    1. patternSymbolPattern[+UnicodeMode, ~UnicodeSetsMode]
  3. 否则,
    1. patternSymbolPattern[~UnicodeMode, ~UnicodeSetsMode]
  4. escapedPattern 为一个 String,其形式为等价于 patternpatternSymbolpattern 被解释为 UTF-16 编码的 Unicode 码点(6.1.4),其中某些码点按下文描述进行转义。escapedPattern 可以与 pattern 不同,也可以不不同;然而,把 escapedPattern 作为 patternSymbol 求值所产生的 Abstract Closure 必须与构造对象的 [[RegExpMatcher]] 内部槽给出的 Abstract Closure 行为相同。使用相同的 patternflags 值多次调用此抽象操作,必须产生相同结果。
  5. 模式中出现的码点 / 或任何 LineTerminator 应根据需要在 escapedPattern 中被转义,以确保 "/"escapedPattern"/"flags字符串连接可以(在适当的词法上下文中)被解析为与构造的正则表达式行为相同的 RegularExpressionLiteral。例如,如果 pattern"/",则 escapedPattern 可以是 "\/""\u002F" 等,但不能是 "/",因为后跟 flags/// 会被解析为 SingleLineComment,而不是 RegularExpressionLiteral。如果 pattern 是空 String,则可通过令 escapedPattern"(?:)" 来满足本规范。
  6. 返回 escapedPattern
Note

尽管名称相似,RegExp.escape 和 EscapeRegExpPattern 并不执行相似的操作。前者转义字符串以便表示在模式内部,而此函数转义模式以便表示为字符串。

22.2.6.14 RegExp.prototype [ %Symbol.split% ] ( string, limit )

Note 1

此方法返回一个 Array,其中存储了将 string 转换为 String 后所得结果的各个子字符串。这些子字符串通过从左到右搜索 this 值正则表达式的匹配来确定;这些出现位置不是返回数组中任何 String 的一部分,而是用来分割该 String 值。

this 值可以是空正则表达式,或可以匹配空 String 的正则表达式。在这种情况下,该正则表达式不会匹配输入 String 开头或结尾处的空子字符串,也不会匹配上一个分隔符匹配末尾处的空子字符串。(例如,如果该正则表达式匹配空 String,则该 String 会被拆分为单个码元元素;结果数组的长度等于该 String 的长度,并且每个子字符串包含一个码元。)只考虑该 String 给定索引处的第一个匹配,即使回溯可能在该索引产生非空子字符串匹配。(例如,/a*?/[Symbol.split]("ab") 求值为数组 ["a", "b"],而 /a*/[Symbol.split]("ab") 求值为数组 ["","b"]。)

如果 string 是(或转换为)空 String,则结果取决于正则表达式是否能匹配空 String。如果可以,结果数组不包含元素。否则,结果数组包含一个元素,即空 String。

如果正则表达式包含捕获括号,则每次匹配 separator 时,捕获括号的结果(包括任何 undefined 结果)都会拼接到输出数组中。例如,

/<(\/)?([^<>]+)>/[Symbol.split]("A<B>bold</B>and<CODE>coded</CODE>")

求值为数组

["A", undefined, "B", "bold", "/", "B", "and", undefined, "CODE", "coded", "/", "CODE", ""]

如果 limit 不是 undefined,则输出数组会被截断,使其包含不超过 limit 个元素。

此方法在被调用时执行以下步骤:

  1. regexpthis 值。
  2. 如果 regexp 不是 Object,则抛出 TypeError 异常。
  3. string 设置为 ? ToString(string)。
  4. speciesCtor 为 ? SpeciesConstructor(regexp, %RegExp%)。
  5. flags 为 ? ToString(? Get(regexp, "flags"))。
  6. 如果 flags 包含 "u"flags 包含 "v",则令 unicodeMatchingtrue
  7. 否则,令 unicodeMatchingfalse
  8. 如果 flags 包含 "y",则令 newFlagsflags
  9. 否则,令 newFlagsflags"y"字符串连接
  10. splitter 为 ? Construct(speciesCtor, « regexp, newFlags »)。
  11. array 为 ! ArrayCreate(0)。
  12. lengthA 为 0。
  13. 如果 limitundefined,则令 lim 为 232 - 1;否则令 lim(? ToUint32(limit))。
  14. 如果 lim = 0,则返回 array
  15. 如果 string 为空 String,则
    1. matchResult 为 ? RegExpExec(splitter, string)。
    2. 如果 matchResult 不是 null,则返回 array
    3. 执行 ! CreateDataPropertyOrThrow(array, "0", string)。
    4. 返回 array
  16. sizestring 的长度。
  17. lastMatchEnd 为 0。
  18. searchIndexlastMatchEnd
  19. 重复,只要 searchIndex < size
    1. 执行 ? Set(splitter, "lastIndex", 𝔽(searchIndex), true)。
    2. matchResult 为 ? RegExpExec(splitter, string)。
    3. 如果 matchResultnull,则
      1. searchIndex 设置为 AdvanceStringIndex(string, searchIndex, unicodeMatching)。
    4. 否则,
      1. matchEnd(? ToLength(? Get(splitter, "lastIndex"))).
      2. matchEnd 设置为 min(matchEnd, size)。
      3. 如果 matchEnd = lastMatchEnd,则
        1. searchIndex 设置为 AdvanceStringIndex(string, searchIndex, unicodeMatching)。
      4. 否则,
        1. substringstringlastMatchEndsearchIndex子字符串
        2. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(lengthA)), substring)。
        3. lengthA 设置为 lengthA + 1。
        4. 如果 lengthA = lim,则返回 array
        5. lastMatchEnd 设置为 matchEnd
        6. numberOfCaptures 为 ? LengthOfArrayLike(matchResult)。
        7. numberOfCaptures 设置为 max(numberOfCaptures - 1, 0)。
        8. captureIndex 为 1。
        9. 重复,只要 captureIndexnumberOfCaptures
          1. nextCapture 为 ? Get(matchResult, ! ToString(𝔽(captureIndex)))。
          2. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(lengthA)), nextCapture)。
          3. captureIndex 设置为 captureIndex + 1。
          4. lengthA 设置为 lengthA + 1。
          5. 如果 lengthA = lim,则返回 array
        10. searchIndex 设置为 lastMatchEnd
  20. substringstringlastMatchEndsize子字符串
  21. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(lengthA)), substring)。
  22. 返回 array

此方法的 "name" 属性的值是 "[Symbol.split]"

Note 2

此方法会忽略此 RegExp 对象的 "global""sticky" 属性的值。

22.2.6.15 get RegExp.prototype.sticky

RegExp.prototype.sticky访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. regexpthis 值。
  2. codeUnit 为码元 0x0079(LATIN SMALL LETTER Y)。
  3. 返回 ? RegExpHasFlag(regexp, codeUnit)。

22.2.6.16 RegExp.prototype.test ( string )

此方法在被调用时执行以下步骤:

  1. regexpthis 值。
  2. 如果 regexp 不是 Object,则抛出 TypeError 异常。
  3. string 设置为 ? ToString(string)。
  4. match 为 ? RegExpExec(regexp, string)。
  5. 如果 matchnull,则返回 false
  6. 返回 true

22.2.6.17 RegExp.prototype.toString ( )

  1. regexpthis 值。
  2. 如果 regexp 不是 Object,则抛出 TypeError 异常。
  3. pattern 为 ? ToString(? Get(regexp, "source"))。
  4. flags 为 ? ToString(? Get(regexp, "flags"))。
  5. result"/"pattern"/"flags字符串连接
  6. 返回 result
Note

返回的 String 具有 RegularExpressionLiteral 的形式,求值后会得到另一个与此对象具有相同行为的 RegExp 对象。

22.2.6.18 get RegExp.prototype.unicode

RegExp.prototype.unicode访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. regexpthis 值。
  2. codeUnit 为码元 0x0075(LATIN SMALL LETTER U)。
  3. 返回 ? RegExpHasFlag(regexp, codeUnit)。

22.2.6.19 get RegExp.prototype.unicodeSets

RegExp.prototype.unicodeSets访问器属性,其 set 访问器函数是 undefined。其 get 访问器函数在被调用时执行以下步骤:

  1. regexpthis 值。
  2. codeUnit 为码元 0x0076(LATIN SMALL LETTER V)。
  3. 返回 ? RegExpHasFlag(regexp, codeUnit)。

22.2.7 RegExp 匹配的抽象操作

22.2.7.1 RegExpExec ( regexp, string )

The abstract operation RegExpExec takes arguments regexp (an Object) and string (a String) and returns either a normal completion containing either an Object or null, or a throw completion. It performs the following steps when called:

  1. exec 为 ? Get(regexp, "exec")。
  2. 如果 IsCallable(exec) 是 true,则
    1. result 为 ? Call(exec, regexp, « string »)。
    2. 如果 result 不是 Object 且 result 不是 null,则抛出 TypeError 异常。
    3. 返回 result
  3. 执行 ? RequireInternalSlot(regexp, [[RegExpMatcher]])。
  4. 返回 ? RegExpBuiltinExec(regexp, string)。
Note

如果未找到可调用的 "exec" 属性,此算法会回退为尝试使用内置 RegExp 匹配算法。这为针对旧版本编写的代码提供了兼容行为;在那些版本中,大多数使用正则表达式的内置算法不会对 "exec" 执行动态属性查找。

22.2.7.2 RegExpBuiltinExec ( regexp, string )

The abstract operation RegExpBuiltinExec takes arguments regexp (an initialized RegExp instance) and string (a String) and returns either a normal completion containing either an Array exotic object or null, or a throw completion. It performs the following steps when called:

  1. lengthstring 的长度。
  2. lastIndex(? ToLength(! Get(regexp, "lastIndex")))。
  3. flagsregexp.[[OriginalFlags]]
  4. 如果 flags 包含 "g",则令 globaltrue;否则令 globalfalse
  5. 如果 flags 包含 "y",则令 stickytrue;否则令 stickyfalse
  6. 如果 flags 包含 "d",则令 hasIndicestrue;否则令 hasIndicesfalse
  7. 如果 globalfalsestickyfalse,则将 lastIndex 设置为 0。
  8. matcherregexp.[[RegExpMatcher]]
  9. 如果 flags 包含 "u"flags 包含 "v",则令 fullUnicodetrue;否则令 fullUnicodefalse
  10. matchSucceededfalse
  11. 如果 fullUnicodetrue,则令 inputStringToCodePoints(string);否则令 input 为一个 List,其元素是 string 的元素所对应的码元。
  12. 注:input 的每个元素都被视为一个字符。
  13. 重复,只要 matchSucceededfalse
    1. 如果 lastIndex > length,则
      1. 如果 globaltruestickytrue,则
        1. 执行 ? Set(regexp, "lastIndex", +0𝔽, true)。
      2. 返回 null
    2. inputIndexinput 中的索引,该索引对应从 string 的元素 lastIndex 获得的字符。
    3. resultmatcher(input, inputIndex)。
    4. 如果 resultfailure,则
      1. 如果 stickytrue,则
        1. 执行 ? Set(regexp, "lastIndex", +0𝔽, true)。
        2. 返回 null
      2. lastIndex 设置为 AdvanceStringIndex(string, lastIndex, fullUnicode)。
    5. 否则,
      1. 断言:resultMatchState
      2. matchSucceeded 设置为 true
  14. endIndexresult.[[EndIndex]]
  15. 如果 fullUnicodetrue,则将 endIndex 设置为 GetStringIndex(string, endIndex)。
  16. 如果 globaltruestickytrue,则
    1. 执行 ? Set(regexp, "lastIndex", 𝔽(endIndex), true)。
  17. capturingGroupsCountresult.[[Captures]] 中元素的数量。
  18. 断言:capturingGroupsCount = regexp.[[RegExpRecord]].[[CapturingGroupsCount]]
  19. 断言:capturingGroupsCount < 232 - 1。
  20. array 为 ! ArrayCreate(capturingGroupsCount + 1)。
  21. 断言:array"length" 属性的数学值capturingGroupsCount + 1。
  22. 执行 ! CreateDataPropertyOrThrow(array, "index", 𝔽(lastIndex))。
  23. 执行 ! CreateDataPropertyOrThrow(array, "input", string)。
  24. matchMatch Record { [[StartIndex]]: lastIndex, [[EndIndex]]: endIndex }。
  25. indices 为新的空 List
  26. groupNames 为新的空 List
  27. match 追加到 indices
  28. matchedSubstringGetMatchString(string, match)。
  29. 执行 ! CreateDataPropertyOrThrow(array, "0", matchedSubstring)。
  30. 如果 regexp 包含任何 GroupName,则
    1. groupsOrdinaryObjectCreate(null)。
    2. hasGroupstrue
  31. 否则,
    1. groupsundefined
    2. hasGroupsfalse
  32. 执行 ! CreateDataPropertyOrThrow(array, "groups", groups)。
  33. matchedGroupNames 为新的空 List
  34. 对每个满足 1 ≤ icapturingGroupsCount 的整数 i,按升序执行:
    1. captureresult.[[Captures]] 的第 ith 个元素。
    2. 如果 captureundefined,则
      1. capturedValueundefined
      2. undefined 追加到 indices
    3. 否则,
      1. captureStartcapture.[[StartIndex]]
      2. captureEndcapture.[[EndIndex]]
      3. 如果 fullUnicodetrue,则
        1. captureStart 设置为 GetStringIndex(string, captureStart)。
        2. captureEnd 设置为 GetStringIndex(string, captureEnd)。
      4. captureRecordMatch Record { [[StartIndex]]: captureStart, [[EndIndex]]: captureEnd }。
      5. capturedValueGetMatchString(string, captureRecord)。
      6. captureRecord 追加到 indices
    4. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(i)), capturedValue)。
    5. 如果 regexp 的第 ith 个 capture 使用 GroupName 定义,则
      1. groupName 为该 GroupNameCapturingGroupName
      2. 如果 matchedGroupNames 包含 groupName,则
        1. 断言:capturedValueundefined
        2. undefined 追加到 groupNames
      3. 否则,
        1. 如果 capturedValue 不是 undefined,则将 groupName 追加到 matchedGroupNames
        2. 注:如果有多个名为 groupName 的组,则此时 groups 可能已经有一个 groupName 属性。不过,由于 groups普通对象,并且其属性全都是可写的数据属性,因此对 CreateDataPropertyOrThrow 的调用仍然保证会成功。
        3. 执行 ! CreateDataPropertyOrThrow(groups, groupName, capturedValue)。
        4. groupName 追加到 groupNames
    6. 否则,
      1. undefined 追加到 groupNames
  35. 如果 hasIndicestrue,则
    1. indicesArrayMakeMatchIndicesIndexPairArray(string, indices, groupNames, hasGroups)。
    2. 执行 ! CreateDataPropertyOrThrow(array, "indices", indicesArray)。
  36. 返回 array

22.2.7.3 AdvanceStringIndex ( string, index, unicode )

The abstract operation AdvanceStringIndex takes arguments string (a String), index (a non-negative integer), and unicode (a Boolean) and returns a non-negative integer. It performs the following steps when called:

  1. 断言:index ≤ 253 - 1。
  2. 如果 unicodefalse,则返回 index + 1。
  3. lengthstring 的长度。
  4. 如果 index + 1 ≥ length,则返回 index + 1。
  5. codePointCodePointAt(string, index)。
  6. 返回 index + codePoint.[[CodeUnitCount]]

22.2.7.4 GetStringIndex ( string, codePointIndex )

The abstract operation GetStringIndex takes arguments string (a String) and codePointIndex (a non-negative integer) and returns a non-negative integer. 它将 string 解释为 UTF-16 编码码点序列,如 6.1.4 中所述,并在对应于码点索引 codePointIndex 的码元索引存在时返回该码元索引。否则,返回 string 的长度。 It performs the following steps when called:

  1. 如果 string 为空 String,则返回 0。
  2. lengthstring 的长度。
  3. codeUnitCount 为 0。
  4. codePointCount 为 0。
  5. 重复,只要 codeUnitCount < length
    1. 如果 codePointCount = codePointIndex,则返回 codeUnitCount
    2. codePointCodePointAt(string, codeUnitCount)。
    3. codeUnitCount 设置为 codeUnitCount + codePoint.[[CodeUnitCount]]
    4. codePointCount 设置为 codePointCount + 1。
  6. 返回 length

22.2.7.5 Match Record

Match Record 是一种 Record 值,用于封装正则表达式匹配或捕获的开始和结束索引。

Match Record 具有 Table 68 中列出的字段。

Table 68: Match Record 字段
字段名 含义
[[StartIndex]] a non-negative integer 从字符串开头起计数,匹配开始处(包含)的码元数量。
[[EndIndex]] an integer[[StartIndex]] 从字符串开头起计数,匹配结束处(不包含)的码元数量。

22.2.7.6 GetMatchString ( string, match )

The abstract operation GetMatchString takes arguments string (a String) and match (a Match Record) and returns a String. It performs the following steps when called:

  1. 断言:match.[[StartIndex]]match.[[EndIndex]]string 的长度。
  2. 返回 stringmatch.[[StartIndex]]match.[[EndIndex]]子字符串

22.2.7.7 GetMatchIndexPair ( string, match )

The abstract operation GetMatchIndexPair takes arguments string (a String) and match (a Match Record) and returns an Array. It performs the following steps when called:

  1. 断言:match.[[StartIndex]]match.[[EndIndex]]string 的长度。
  2. 返回 CreateArrayFromList𝔽(match.[[StartIndex]]), 𝔽(match.[[EndIndex]]) »)。

22.2.7.8 MakeMatchIndicesIndexPairArray ( string, indices, groupNames, hasGroups )

The abstract operation MakeMatchIndicesIndexPairArray takes arguments string (a String), indices (a List of either Match Records or undefined), groupNames (a List of either Strings or undefined), and hasGroups (a Boolean) and returns an Array. It performs the following steps when called:

  1. nindices 中元素的数量。
  2. 断言:n < 232 - 1。
  3. 断言:groupNamesn - 1 个元素。
  4. 注:groupNames List 包含indices List 中从 indices[1] 开始对齐的元素。
  5. array 为 ! ArrayCreate(n)。
  6. 如果 hasGroupstrue,则
    1. groupsOrdinaryObjectCreate(null)。
  7. 否则,
    1. groupsundefined
  8. 执行 ! CreateDataPropertyOrThrow(array, "groups", groups)。
  9. 对每个满足 0 ≤ i < n 的整数 i,按升序执行:
    1. matchIndicesindices[i]。
    2. 如果 matchIndices 不是 undefined,则
      1. matchIndexPairGetMatchIndexPair(string, matchIndices)。
    3. 否则,
      1. matchIndexPairundefined
    4. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(i)), matchIndexPair)。
    5. 如果 i > 0,则
      1. namegroupNames[i - 1]。
      2. 如果 name 不是 undefined,则
        1. 断言:groups 不是 undefined
        2. 注:如果有多个名为 name 的组,则此时 groups 可能已经有一个 name 属性。不过,由于 groups普通对象,并且其属性全都是可写的数据属性,因此对 CreateDataPropertyOrThrow 的调用仍然保证会成功。
        3. 执行 ! CreateDataPropertyOrThrow(groups, name, matchIndexPair)。
  10. 返回 array

22.2.8 RegExp 实例的属性

RegExp 实例是普通对象,继承自 RegExp 原型对象的属性。RegExp 实例具有内部槽 [[OriginalSource]][[OriginalFlags]][[RegExpRecord]][[RegExpMatcher]][[RegExpMatcher]] 内部槽的值是 RegExp 对象的 PatternAbstract Closure 表示。

Note

在 ECMAScript 2015 之前,RegExp 实例被规定为具有自身数据属性 "source""global""ignoreCase""multiline"。这些属性现在被规定为 RegExp.prototype访问器属性

RegExp 实例还具有以下属性:

22.2.8.1 lastIndex

"lastIndex" 属性的值指定下一次匹配开始处的 String 索引。使用时会将其强制转换为整数 Number(见 22.2.7.2)。此属性应具有特性 { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }。

22.2.9 RegExp String Iterator 对象

RegExp String Iterator 是表示对某个特定 String 实例对象进行特定迭代,并与某个特定 RegExp 实例对象匹配的对象。RegExp String Iterator 对象没有具名构造器。相反,RegExp String Iterator 对象通过调用 RegExp 实例对象的某些方法创建。

22.2.9.1 CreateRegExpStringIterator ( regexp, string, global, fullUnicode )

The abstract operation CreateRegExpStringIterator takes arguments regexp (an Object), string (a String), global (a Boolean), and fullUnicode (a Boolean) and returns an Object. It performs the following steps when called:

  1. iteratorOrdinaryObjectCreate(%RegExpStringIteratorPrototype%, « [[IteratingRegExp]], [[IteratedString]], [[Global]], [[Unicode]], [[Done]] »)。
  2. iterator.[[IteratingRegExp]] 设置为 regexp
  3. iterator.[[IteratedString]] 设置为 string
  4. iterator.[[Global]] 设置为 global
  5. iterator.[[Unicode]] 设置为 fullUnicode
  6. iterator.[[Done]] 设置为 false
  7. 返回 iterator

22.2.9.2 %RegExpStringIteratorPrototype% 对象

%RegExpStringIteratorPrototype% 对象:

22.2.9.2.1 %RegExpStringIteratorPrototype%.next ( )

  1. iteratorObjthis 值。
  2. 如果 iteratorObj 不是 Object,则抛出 TypeError 异常。
  3. 如果 iteratorObj 不具有 RegExp String Iterator Object Instance 的所有内部槽(见 22.2.9.3),则抛出 TypeError 异常。
  4. 如果 iteratorObj.[[Done]]true,则
    1. 返回 CreateIteratorResultObject(undefined, true)。
  5. regexpiteratorObj.[[IteratingRegExp]]
  6. stringiteratorObj.[[IteratedString]]
  7. globaliteratorObj.[[Global]]
  8. fullUnicodeiteratorObj.[[Unicode]]
  9. match 为 ? RegExpExec(regexp, string)。
  10. 如果 matchnull,则
    1. iteratorObj.[[Done]] 设置为 true
    2. 返回 CreateIteratorResultObject(undefined, true)。
  11. 如果 globalfalse,则
    1. iteratorObj.[[Done]] 设置为 true
    2. 返回 CreateIteratorResultObject(match, false)。
  12. matchString 为 ? ToString(? Get(match, "0"))。
  13. 如果 matchString 为空 String,则
    1. thisIndex(? ToLength(? Get(regexp, "lastIndex"))).
    2. nextIndexAdvanceStringIndex(string, thisIndex, fullUnicode)。
    3. 执行 ? Set(regexp, "lastIndex", 𝔽(nextIndex), true)。
  14. 返回 CreateIteratorResultObject(match, false)。

22.2.9.2.2 %RegExpStringIteratorPrototype% [ %Symbol.toStringTag% ]

%Symbol.toStringTag% 属性的初始值是 String 值 "RegExp String Iterator"

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }。

22.2.9.3 RegExp String Iterator 实例的属性

RegExp String Iterator 实例是普通对象,继承自 %RegExpStringIteratorPrototype% 内置对象的属性。RegExp String Iterator 实例最初以 Table 69 中列出的内部槽创建。

Table 69: RegExp String Iterator 实例的内部槽
内部槽 类型 描述
[[IteratingRegExp]] an Object 用于迭代的正则表达式。IsRegExp([[IteratingRegExp]]) 初始为 true
[[IteratedString]] a String 正在迭代的 String 值。
[[Global]] a Boolean 指示 [[IteratingRegExp]] 是否为全局。
[[Unicode]] a Boolean 指示 [[IteratingRegExp]] 是否处于 Unicode 模式。
[[Done]] a Boolean 指示迭代是否完成。