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. str 为空 String。
  2. 否则,
    1. 如果 NewTarget 是 undefinedvalue 是一个 Symbol,返回 SymbolDescriptiveString(value)。
    2. str 为 ? ToString(value)。
  3. 如果 NewTarget 是 undefined,返回 str
  4. 返回 StringCreate(str, ? GetPrototypeFromConstructor(NewTarget, "%String.prototype%"))。

22.1.2 String 构造器的属性

String 构造器

22.1.2.1 String.fromCharCode ( ...codeUnits )

此函数可使用任意数量的参数调用,这些参数构成剩余参数 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 )

此函数可使用任意数量的参数调用,这些参数构成剩余参数 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. nextLiteralVal 为 ? Get(literals, ! ToString(𝔽(nextIndex)))。
    2. nextLiteral 为 ? ToString(nextLiteralVal)。
    3. result 设置为 resultnextLiteral 的字符串拼接。
    4. 如果 nextIndex + 1 = literalCount,返回 result
    5. 如果 nextIndex < substitutionCount,则
      1. nextSubValsubstitutions[nextIndex]。
      2. nextSub 为 ? ToString(nextSubVal)。
      3. result 设置为 resultnextSub 的字符串拼接。
    6. nextIndex 设置为 nextIndex + 1。
Note

此函数旨在用作 Tagged Template(13.3.11)的标签函数。当如此调用时,第一个参数将是一个良构模板对象,剩余参数将包含替换值。

22.1.3 String 原型对象的属性

String 原型对象

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

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

22.1.3.1 String.prototype.at ( index )

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. lenstr 的长度。
  5. relativeIndex 为 ? ToIntegerOrInfinity(index)。
  6. 如果 relativeIndex ≥ 0,则
    1. krelativeIndex
  7. 否则,
    1. klen + relativeIndex
  8. 如果 k < 0 或 klen,返回 undefined
  9. 返回 str 中从 kk + 1 的子字符串

22.1.3.2 String.prototype.charAt ( pos )

Note 1

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

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

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

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. position 为 ? ToIntegerOrInfinity(pos)。
  5. sizestr 的长度。
  6. 如果 position < 0 或 positionsize,返回空 String。
  7. 返回 str 中从 positionposition + 1 的子字符串
Note 2

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

22.1.3.3 String.prototype.charCodeAt ( pos )

Note 1

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

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

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

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

22.1.3.4 String.prototype.codePointAt ( pos )

Note 1

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

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

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. position 为 ? ToIntegerOrInfinity(pos)。
  5. sizestr 的长度。
  6. 如果 position < 0 或 positionsize,返回 undefined
  7. cpCodePointAt(str, position)。
  8. 返回 𝔽(cp.[[CodePoint]])。
Note 2

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

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

Note 1

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

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

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. resultstr
  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 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. isRegExp 为 ? IsRegExp(searchString)。
  5. 如果 isRegExptrue,抛出 TypeError 异常。
  6. searchStr 为 ? ToString(searchString)。
  7. lenstr 的长度。
  8. 如果 endPositionundefined,令 poslen;否则令 pos 为 ? ToIntegerOrInfinity(endPosition)。
  9. end 为将 pos 夹在 0 和 len 之间的结果。
  10. searchLengthsearchStr 的长度。
  11. 如果 searchLength = 0,返回 true
  12. startend - searchLength
  13. 如果 start < 0,返回 false
  14. substringstr 中从 startend子字符串
  15. 如果 substringsearchStr,返回 true
  16. 返回 false
Note 1

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

Note 2

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

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

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

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. isRegExp 为 ? IsRegExp(searchString)。
  5. 如果 isRegExptrue,抛出 TypeError 异常。
  6. searchStr 为 ? ToString(searchString)。
  7. pos 为 ? ToIntegerOrInfinity(position)。
  8. 断言:如果 positionundefined,则 pos 为 0。
  9. lenstr 的长度。
  10. start 为将 pos 夹在 0 和 len 之间的结果。
  11. indexStringIndexOf(str, searchStr, start)。
  12. 如果 indexnot-found,返回 false
  13. 返回 true
Note 1

如果 searchString 作为将此对象转换为 String 后所得结果的一个 substring 出现在一个或多个大于或等于 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 后所得结果的一个 substring 出现在一个或多个大于或等于 position 的索引处,则返回此类索引中最小的一个;否则返回 -1𝔽。如果 positionundefined,则假定为 +0𝔽,以便搜索整个 String。

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

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. searchStr 为 ? ToString(searchString)。
  5. pos 为 ? ToIntegerOrInfinity(position)。
  6. 断言:如果 positionundefined,则 pos 为 0。
  7. lenstr 的长度。
  8. start 为将 pos 夹在 0 和 len 之间的结果。
  9. resultStringIndexOf(str, searchStr, start)。
  10. 如果 resultnot-found,返回 -1𝔽
  11. 返回 𝔽(result)。
Note 2

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

22.1.3.10 String.prototype.isWellFormed ( )

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

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

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

Note 1

如果 searchString 作为将此对象转换为 String 后所得结果的一个 substring 出现在一个或多个小于或等于 position 的索引处,则返回此类索引中最大的一个;否则返回 -1𝔽。如果 positionundefined,则假定为 String 值的长度,以便搜索整个 String。

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

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. searchStr 为 ? ToString(searchString)。
  5. numPos 为 ? ToNumber(position)。
  6. 断言:如果 positionundefined,则 numPosNaN
  7. 如果 numPosNaN,令 pos 为 +∞;否则令 pos 为 ! ToIntegerOrInfinity(numPos)。
  8. lenstr 的长度。
  9. searchLensearchStr 的长度。
  10. 如果 len < searchLen,返回 -1𝔽
  11. start 为将 pos 夹在 0 和 len - searchLen 之间的结果。
  12. resultStringLastIndexOf(str, searchStr, 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 str)与 that(转换为 String thatValue)进行实现定义的区域设置敏感 String 比较的结果。该结果旨在对应于按照宿主环境当前区域设置的约定对 String 值进行排序的顺序;当 str 排在 thatValue 之前时为负,当 str 排在 thatValue 之后时为正,在所有其他情况下为零(表示 strthatValue 之间没有相对顺序)。

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

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

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

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

Note 1

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

Note 2

此方法可以依赖 ECMAScript 环境从宿主环境获得的任何语言和/或区域设置敏感比较功能,并旨在按照宿主环境当前区域设置的约定进行比较。然而,无论比较能力如何,此方法都必须识别并遵守 Unicode 标准中的规范等价性——例如,以下比较必须全部返回 +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 标准第 2 章和第 3 章,以及 Unicode Standard Annex #15, Unicode Normalization FormsUnicode Technical Note #5, Canonical Equivalence in Applications。另见 Unicode Technical Standard #10, Unicode Collation Algorithm

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

Note 3

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

22.1.3.13 String.prototype.match ( regexpOrPattern )

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

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

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

22.1.3.14 String.prototype.matchAll ( regexpOrPattern )

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

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

  1. thisValuethis 值。
  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. str 为 ? ToString(thisValue)。
  5. regexp 为 ? RegExpCreate(regexpOrPattern, "g")。
  6. 返回 ? Invoke(regexp, %Symbol.matchAll%, « str »)。
Note 1
此方法有意设计为泛型,它不要求其 this 值是 String 对象。因此,它可以被转移到其他种类的对象上作为方法使用。
Note 2
类似于 String.prototype.splitString.prototype.matchAll 被设计为通常不改变其输入。

22.1.3.15 String.prototype.normalize ( [ form ] )

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

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. 如果 formundefined,令 f"NFC"
  5. 否则,令 f 为 ? ToString(form)。
  6. 如果 f 不是 "NFC""NFD""NFKC""NFKD" 之一,抛出 RangeError 异常。
  7. ns 为 String 值,该值是按照 最新 Unicode 标准的 Normalization Forms 中所指定的、将 str 规范化为名为 f 的规范化形式的结果。
  8. 返回 ns
Note

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

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

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

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

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

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

  1. thisValuethis 值。
  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 (一个 ECMAScript 语言值,), maxLength (一个 ECMAScript 语言值,), fillString (一个 ECMAScript 语言值,), and placement (startend,) and returns 要么是一个包含 String 的正常完成,要么是一个抛出完成. It performs the following steps when called:

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

22.1.3.17.2 StringPad ( str, maxLength, fillString, placement )

The abstract operation StringPad takes arguments str (一个 String,), maxLength (一个非负整数,), fillString (一个 String,), and placement (startend,) and returns 一个 String. It performs the following steps when called:

  1. stringLengthstr 的长度。
  2. 如果 maxLengthstringLength,返回 str
  3. 如果 fillString 是空 String,返回 str
  4. fillLenmaxLength - stringLength
  5. truncatedStringFiller 为由重复拼接 fillString 并截断到长度 fillLen 所组成的 String 值。
  6. 如果 placementstart,返回 truncatedStringFillerstr 的字符串拼接。
  7. 返回 strtruncatedStringFiller 的字符串拼接。
Note 1

参数 maxLength 将被夹取,使其不可能小于 str 的长度。

Note 2

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

22.1.3.17.3 ToZeroPaddedDecimalString ( n, minLength )

The abstract operation ToZeroPaddedDecimalString takes arguments n (一个非负整数,) and minLength (一个非负整数,) and returns 一个 String. It performs the following steps when called:

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

22.1.3.18 String.prototype.repeat ( count )

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

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

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

Note 2

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

22.1.3.19 String.prototype.replace ( searchValue, replaceValue )

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

  1. thisValuethis 值。
  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. followingstring 中从 position + 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, str, position, captures, namedCaptures, replacementTemplate )

The abstract operation GetSubstitution takes arguments matched (一个 String,), str (一个 String,), position (一个非负整数,), captures (一个由 String 或 undefined 组成的 List,), namedCaptures (一个 Object 或 undefined,), and replacementTemplate (一个 String,) and returns 要么是一个包含 String 的正常完成,要么是一个抛出完成. 对于此抽象操作而言,十进制数字是位于从 0x0030 (DIGIT ZERO) 到 0x0039 (DIGIT NINE) 的闭区间内的码元。 It performs the following steps when called:

  1. stringLengthstr 的长度。
  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. refReplacementstr 中从 0 到 position子字符串
    4. 否则如果 templateRemainder"$&" 开始,则
      1. ref"$&"
      2. refReplacementmatched
    5. 否则如果 templateRemainder"$'"(0x0024 (DOLLAR SIGN) 后跟 0x0027 (APOSTROPHE))开始,则
      1. ref"$'"
      2. matchLengthmatched 的长度。
      3. tailPosposition + matchLength
      4. refReplacementstr 中从 min(tailPos, stringLength) 开始的子字符串
      5. 注:只有当此抽象操作是由对象上 %RegExp.prototype% 的内在 %Symbol.replace% 方法调用,且该对象的 "exec" 属性不是内在 %RegExp.prototype.exec% 时,tailPos 才可能超过 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. captureLencaptures 中元素的数量。
      6. 如果 index > captureLendigitCount = 2,则
        1. 注:当一个两位数替换模式指定了超过捕获组数量的索引时,它被视为一个一位数替换模式后跟一个字面数字。
        2. digitCount 设置为 1。
        3. digits 设置为 digits 中从 0 到 1 的子字符串
        4. index 设置为 (StringToNumber(digits))。
      7. reftemplateRemainder 中从 0 到 1 + digitCount子字符串
      8. 如果 1 ≤ indexcaptureLen,则
        1. capturecaptures[index - 1]。
        2. 如果 captureundefined,则
          1. refReplacement 为空 String。
        3. 否则,
          1. refReplacementcapture
      9. 否则,
        1. refReplacementref
    7. 否则如果 templateRemainder"$<" 开始,则
      1. gtPosStringIndexOf(templateRemainder, ">", 0)。
      2. 如果 gtPosnot-foundnamedCapturesundefined,则
        1. ref"$<"
        2. refReplacementref
      3. 否则,
        1. reftemplateRemainder 中从 0 到 gtPos + 1 的子字符串
        2. groupNametemplateRemainder 中从 2 到 gtPos子字符串
        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 设置为 templateRemainder 中从 refLength 开始的子字符串
    11. result 设置为 resultrefReplacement 的字符串拼接。
  6. 返回 result

22.1.3.20 String.prototype.replaceAll ( searchValue, replaceValue )

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

  1. thisValuethis 值。
  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. preservedstring 中从 endOfLastMatchmatchPosition子字符串
    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 设置为 resultstring 中从 endOfLastMatch 开始的子字符串的字符串拼接。
  17. 返回 result

22.1.3.21 String.prototype.search ( regexpOrPattern )

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

  1. thisValuethis 值。
  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 后所得结果的一个 substring,该子字符串从索引 start 开始并持续到但不包括索引 end(如果 endundefined,则一直到 String 末尾)。如果 start 为负,则将其视为 sourceLength + start,其中 sourceLength 是 String 的长度。如果 end 为负,则将其视为 sourceLength + end,其中 sourceLength 是 String 的长度。结果是 String 值,而不是 String 对象。

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

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

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

22.1.3.23 String.prototype.split ( separator, limit )

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

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

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. 如果 separator 是一个 Object,则
    1. splitter 为 ? GetMethod(separator, %Symbol.split%)。
    2. 如果 splitter 不是 undefined,则
      1. 返回 ? Call(splitter, separator, « thisValue, limit »)。
  4. str 为 ? ToString(thisValue)。
  5. 如果 limitundefined,令 lim 为 232 - 1;否则令 lim(? ToUint32(limit))。
  6. separatorStr 为 ? ToString(separator)。
  7. 如果 lim = 0,则
    1. 返回 CreateArrayFromList(« »)。
  8. 如果 separatorundefined,则
    1. 返回 CreateArrayFromListstr »)。
  9. separatorLengthseparatorStr 的长度。
  10. 如果 separatorLength = 0,则
    1. strLenstr 的长度。
    2. outLen 为将 lim 夹在 0 和 strLen 之间的结果。
    3. headstr 中从 0 到 outLen子字符串
    4. codeUnits 为一个 List,它由作为 head 元素的码元序列组成。
    5. 返回 CreateArrayFromList(codeUnits)。
  11. 如果 str 是空 String,返回 CreateArrayFromListstr »)。
  12. substrings 为一个新的空 List
  13. searchStart 为 0。
  14. matchIndexStringIndexOf(str, separatorStr, 0)。
  15. 重复,当 matchIndex 不是 not-found 时,
    1. substringstr 中从 searchStartmatchIndex子字符串
    2. substring 追加到 substrings
    3. 如果 substrings 中元素的数量是 lim,返回 CreateArrayFromList(substrings)。
    4. searchStart 设置为 matchIndex + separatorLength
    5. matchIndex 设置为 StringIndexOf(str, separatorStr, searchStart)。
  16. substringstr 中从 searchStart 开始的子字符串
  17. substring 追加到 substrings
  18. 返回 CreateArrayFromList(substrings)。
Note 1

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

如果 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 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. isRegExp 为 ? IsRegExp(searchString)。
  5. 如果 isRegExptrue,抛出 TypeError 异常。
  6. searchStr 为 ? ToString(searchString)。
  7. lenstr 的长度。
  8. 如果 positionundefined,令 pos 为 0;否则令 pos 为 ? ToIntegerOrInfinity(position)。
  9. start 为将 pos 夹在 0 和 len 之间的结果。
  10. searchLengthsearchStr 的长度。
  11. 如果 searchLength = 0,返回 true
  12. endstart + searchLength
  13. 如果 end > len,返回 false
  14. substringstr 中从 startend子字符串
  15. 如果 substringsearchStr,返回 true
  16. 返回 false
Note 1

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

Note 2

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

22.1.3.25 String.prototype.substring ( start, end )

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

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

如果 start 严格大于 end,则交换它们。

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

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. lenstr 的长度。
  5. intStart 为 ? ToIntegerOrInfinity(start)。
  6. 如果 endundefined,令 intEndlen;否则令 intEnd 为 ? ToIntegerOrInfinity(end)。
  7. finalStart 为将 intStart 夹在 0 和 len 之间的结果。
  8. finalEnd 为将 intEnd 夹在 0 和 len 之间的结果。
  9. frommin(finalStart, finalEnd)。
  10. tomax(finalStart, finalEnd)。
  11. 返回 str 中从 fromto子字符串
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 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. sTextStringToCodePoints(str)。
  5. lowerText 为按照 Unicode 默认大小写转换算法得到的 toLowercase(sText)。
  6. lowercaseStringCodePointsToString(lowerText)。
  7. 返回 lowercaseString

结果必须根据 Unicode 字符数据库中的非区域设置敏感大小写映射派生(这明确不仅包括文件 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 完全相同,只是使用 Unicode 默认大小写转换的 toUppercase 算法来映射 String。

Note

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

22.1.3.31 String.prototype.toWellFormed ( )

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

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

  1. thisValuethis 值。
  2. 执行 ? RequireObjectCoercible(thisValue)。
  3. str 为 ? ToString(thisValue)。
  4. strLenstr 的长度。
  5. k 为 0。
  6. result 为空 String。
  7. 重复,当 k < strLen 时,
    1. cpCodePointAt(str, k)。
    2. 如果 cp.[[IsUnpairedSurrogate]]true,则
      1. result 设置为 result 与 0xFFFD (REPLACEMENT CHARACTER) 的字符串拼接。
    3. 否则,
      1. result 设置为 resultUTF16EncodeCodePoint(cp.[[CodePoint]]) 的字符串拼接。
    4. k 设置为 k + cp.[[CodeUnitCount]]
  8. 返回 result

22.1.3.32 String.prototype.trim ( )

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

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

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

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

22.1.3.32.1 TrimString ( string, where )

The abstract operation TrimString takes arguments string (一个 ECMAScript 语言值,) and where (startendstart+end,) and returns 要么是一个包含 String 的正常完成,要么是一个抛出完成. 它将 string 解释为 UTF-16 编码码点的序列,如 6.1.4 中所述。 It performs the following steps when called:

  1. 执行 ? RequireObjectCoercible(string)。
  2. str 为 ? ToString(string)。
  3. 如果 wherestart,则
    1. trimmedStringstr 的一个副本所成的 String 值,其中前导空白已被移除。
  4. 否则如果 whereend,则
    1. trimmedStringstr 的一个副本所成的 String 值,其中尾随空白已被移除。
  5. 否则,
    1. 断言:wherestart+end
    2. trimmedStringstr 的一个副本所成的 String 值,其中前导和尾随空白都已被移除。
  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. strthis 值。
  2. 返回 ? TrimString(str, end)。
Note

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

22.1.3.34 String.prototype.trimStart ( )

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

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

  1. strthis 值。
  2. 返回 ? TrimString(str, start)。
Note

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

22.1.3.35 String.prototype.valueOf ( )

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

  1. 返回 ? ThisStringValue(this value)。

22.1.3.35.1 ThisStringValue ( value )

The abstract operation ThisStringValue takes argument value (一个 ECMAScript 语言值,) and returns 要么是一个包含 String 的正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. 如果 value 是一个 String,返回 value
  2. 如果 value 是一个 Object 且 value 具有 [[StringData]] 内部槽,则
    1. strvalue.[[StringData]]
    2. 断言:str 是一个 String。
    3. 返回 str
  3. 抛出 TypeError 异常。

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

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

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

  1. strthis 值。
  2. 执行 ? RequireObjectCoercible(str)。
  3. str 设置为 ? ToString(str)。
  4. closure 为一个没有参数的新 Abstract Closure,它捕获 str,并在被调用时执行以下步骤:
    1. lenstr 的长度。
    2. position 为 0。
    3. 重复,当 position < len 时,
      1. cpCodePointAt(str, position)。
      2. nextIndexposition + cp.[[CodeUnitCount]]
      3. resultStringstr 中从 positionnextIndex子字符串
      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 HexLeadSurrogate 选择存在歧义的 \u HexTrailSurrogate,都应与最近的可能的 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 ( node )

The abstract operation CountLeftCapturingParensWithin takes argument node (一个 Parse Node,) and returns 一个非负整数. 它返回 node 中左捕获括号的数量。左捕获括号是任意由 Atom :: ( GroupSpecifieropt Disjunction ) 产生式的 ( 终结符所匹配的 ( 模式字符。

Note

本节在 B.1.2.2 中被修订。

It performs the following steps when called:

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

22.2.1.3 Static Semantics: CountLeftCapturingParensBefore ( node )

The abstract operation CountLeftCapturingParensBefore takes argument node (一个 Parse Node,) and returns 一个非负整数. 它返回外围 pattern 中位于 node 左侧的左捕获括号的数量。

Note

本节在 B.1.2.2 中被修订。

It performs the following steps when called:

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

22.2.1.4 Static Semantics: MightBothParticipate ( x, y )

The abstract operation MightBothParticipate takes arguments x (一个 Parse Node,) and y (一个 Parse Node,) and returns 一个 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 一个正整数.

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 一个 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 一个非负整数.

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. ch 为由 SourceCharacter 匹配的码点。
  2. 返回 ch 的数值。
ClassEscape :: b
  1. 返回 U+0008 (BACKSPACE) 的数值。
ClassEscape :: -
  1. 返回 U+002D (HYPHEN-MINUS) 的数值。
CharacterEscape :: ControlEscape
  1. 返回根据 Table 62 的数值。
Table 62: 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. ch 为由 AsciiLetter 匹配的码点。
  2. ich 的数值。
  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. cpUTF16SurrogatePairToCodePoint(lead, trail)。
  4. 返回 cp 的数值。
RegExpUnicodeEscapeSequence :: u Hex4Digits
  1. 返回 Hex4Digits 的 MV。
RegExpUnicodeEscapeSequence :: u{ CodePoint }
  1. 返回 CodePoint 的 MV。
HexLeadSurrogate :: Hex4Digits HexTrailSurrogate :: Hex4Digits HexNonSurrogate :: Hex4Digits
  1. 返回 Hex4Digits 的 MV。
CharacterEscape :: IdentityEscape
  1. ch 为由 IdentityEscape 匹配的码点。
  2. 返回 ch 的数值。
ClassSetCharacter :: SourceCharacter but not ClassSetSyntaxCharacter
  1. ch 为由 SourceCharacter 匹配的码点。
  2. 返回 ch 的数值。
ClassSetCharacter :: \ ClassSetReservedPunctuator
  1. ch 为由 ClassSetReservedPunctuator 匹配的码点。
  2. 返回 ch 的数值。
ClassSetCharacter :: \b
  1. 返回 U+0008 (BACKSPACE) 的数值。

22.2.1.8 Static Semantics: MayContainStrings

The syntax-directed operation MayContainStrings takes no arguments and returns 一个 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 66 的 “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 (一个 GroupName Parse Node,) and returns 一个 GroupSpecifier Parse Node 的 List. It performs the following steps when called:

  1. namethisGroupNameCapturingGroupName
  2. pattern 为包含 thisGroupNamePattern
  3. result 为一个新的空 List
  4. 对于 pattern 包含的每个 GroupSpecifier gs,执行
    1. 如果 gsCapturingGroupNamename,则
      1. gs 追加到 result
  5. 返回 result

22.2.1.10 Static Semantics: CapturingGroupName

The syntax-directed operation CapturingGroupName takes no arguments and returns 一个 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 一个码点 List. It is defined piecewise over the following productions:

RegExpIdentifierName :: RegExpIdentifierStart
  1. cpRegExpIdentifierStartRegExpIdentifierCodePoint
  2. 返回 « cp »。
RegExpIdentifierName :: RegExpIdentifierName RegExpIdentifierPart
  1. cps 为派生的 RegExpIdentifierNameRegExpIdentifierCodePoints
  2. cpRegExpIdentifierPartRegExpIdentifierCodePoint
  3. 返回 cps 与 « cp » 的列表拼接。

22.2.1.12 Static Semantics: RegExpIdentifierCodePoint

The syntax-directed operation RegExpIdentifierCodePoint takes no arguments and returns 一个码点. 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 位值是基本多文种平面范围内的 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,其唯一元素是单个模式字符,即码点 U+1D11E。

实现实际上可以不执行这种到 UTF-16 或从 UTF-16 的转换,但本规范的语义要求模式匹配的结果仿佛这些转换已经执行。

22.2.2.1 记法

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

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

22.2.2.1.1 RegExp Record

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

它具有以下字段:

Table 63: RegExp Record 字段
字段名 含义
[[IgnoreCase]] 一个 Boolean 指示 RegExp 的标志中是否出现 "i"
[[Multiline]] 一个 Boolean 指示 RegExp 的标志中是否出现 "m"
[[DotAll]] 一个 Boolean 指示 RegExp 的标志中是否出现 "s"
[[Unicode]] 一个 Boolean 指示 RegExp 的标志中是否出现 "u"
[[UnicodeSets]] 一个 Boolean 指示 RegExp 的标志中是否出现 "v"
[[CapturingGroupsCount]] 一个非负整数 RegExp 的模式中左捕获括号的数量

22.2.2.2 Runtime Semantics: CompilePattern

The syntax-directed operation CompilePattern takes argument regexpRecord (一个 RegExp Record,) and returns 一个 Abstract Closure,该闭包接受一个字符 List 和一个非负整数,并返回一个 MatchStatefailure. 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. 断言:y 是一个 MatchState
      2. 返回 y
    4. cap 为由 regexpRecord.[[CapturingGroupsCount]]undefined 值组成的 List,索引从 1 到 regexpRecord.[[CapturingGroupsCount]]
    5. xMatchState { [[Input]]: input, [[EndIndex]]: index, [[Captures]]: cap }。
    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 (一个 RegExp Record,) and direction (forwardbackward,) and returns 一个 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

| 正则表达式运算符分隔两个替代项。模式首先尝试匹配左侧 Alternative(后跟正则表达式的后续部分);如果失败,则尝试匹配右侧 Disjunction(后跟正则表达式的后续部分)。如果左侧 Alternative、右侧 Disjunction 和后续部分都具有选择点,则在转向左侧 Alternative 的下一个选择之前,会先尝试后续部分中的所有选择。如果左侧 Alternative 中的选择被耗尽,则尝试右侧 Disjunction 而不是左侧 Alternative。模式中被 | 跳过的部分内的任何捕获括号都会产生 undefined 值而不是 String。因此,例如,

/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]

尝试这两个替代项的顺序独立于 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 的下一个选择之前,会先尝试后续部分中的所有选择;在转向左侧 Alternative 的下一个选择之前,会先尝试右侧 Term 中的所有选择。当 directionbackward 时,AlternativeTerm 的求值顺序相反。

Term :: Assertion
  1. 返回以 regexpRecord 为参数的 AssertionCompileAssertion
Note 4

得到的 Matcher 独立于 direction

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. 断言:x 是一个 MatchState
    2. 断言:c 是一个 MatcherContinuation
    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 (一个 Matcher,), min (一个非负整数,), max (一个非负整数或 +∞,), greedy (一个 Boolean,), matchState (一个 MatchState,), continue (一个 MatcherContinuation,), parenIndex (一个非负整数,), and parenCount (一个非负整数,) and returns 一个 MatchStatefailure. It performs the following steps when called:

  1. 如果 max = 0,返回 continue(matchState)。
  2. d 为一个新的 MatcherContinuation,其参数为 (y),捕获 mminmaxgreedymatchStatecontinueparenIndexparenCount,并在被调用时执行以下步骤:
    1. 断言:y 是一个 MatchState
    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. capmatchState.[[Captures]] 的副本。
  4. 对于从 parenIndex + 1 到 parenIndex + parenCount 闭区间内的每个整数 k,将 cap[k] 设置为 undefined
  5. inputmatchState.[[Input]]
  6. ematchState.[[EndIndex]]
  7. xrMatchState { [[Input]]: input, [[EndIndex]]: e, [[Captures]]: cap }。
  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

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

Note 2

如果 Atom 和正则表达式的后续部分都具有选择点,则首先使 Atom 匹配尽可能多(或者如果非贪婪则尽可能少)的次数。在转向 Atom 最后一次重复中的下一个选择之前,会先尝试后续部分中的所有选择。在转向 Atom 倒数第二次(第 n - 1 次)重复中的下一个选择之前,会先尝试 Atom 最后一次(第 nth 次)重复中的所有选择;此时可能会发现现在可以进行更多或更少次 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 的捕获。我们可以在正则表达式

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

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

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

而不是

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

因为最外层 * 的每次迭代都会清除量化 Atom 中包含的所有捕获 String,在本例中包括编号为 2、3、4 和 5 的捕获 String。

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 一个 Matcher. It performs the following steps when called:

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

22.2.2.3.3 MatchTwoAlternatives ( m1, m2 )

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

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

22.2.2.3.4 MatchSequence ( m1, m2, direction )

The abstract operation MatchSequence takes arguments m1 (一个 Matcher,), m2 (一个 Matcher,), and direction (forwardbackward,) and returns 一个 Matcher. It performs the following steps when called:

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

22.2.2.4 Runtime Semantics: CompileAssertion

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

Note 1

本节在 B.1.2.6 中被修订。

It is defined piecewise over the following productions:

Assertion :: ^
  1. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 regexpRecord,并在被调用时执行以下步骤:
    1. 断言:matchState 是一个 MatchState
    2. 断言:continue 是一个 MatcherContinuation
    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. 断言:matchState 是一个 MatchState
    2. 断言:continue 是一个 MatcherContinuation
    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. 断言:matchState 是一个 MatchState
    2. 断言:continue 是一个 MatcherContinuation
    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. 断言:matchState 是一个 MatchState
    2. 断言:continue 是一个 MatcherContinuation
    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. 断言:matchState 是一个 MatchState
    2. 断言:continue 是一个 MatcherContinuation
    3. d 为一个新的 MatcherContinuation,其参数为 (y),不捕获任何内容,并在被调用时执行以下步骤:
      1. 断言:y 是一个 MatchState
      2. 返回 y
    4. rm(matchState, d)。
    5. 如果 rfailure,返回 failure
    6. 断言:r 是一个 MatchState
    7. capr.[[Captures]]
    8. inputmatchState.[[Input]]
    9. xematchState.[[EndIndex]]
    10. zMatchState { [[Input]]: input, [[EndIndex]]: xe, [[Captures]]: cap }。
    11. 返回 continue(z)。
Note 3

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

例如,

/(?=(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. 断言:matchState 是一个 MatchState
    2. 断言:continue 是一个 MatcherContinuation
    3. d 为一个新的 MatcherContinuation,其参数为 (y),不捕获任何内容,并在被调用时执行以下步骤:
      1. 断言:y 是一个 MatchState
      2. 返回 y
    4. rm(matchState, d)。
    5. 如果 r 不是 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. 断言:matchState 是一个 MatchState
    2. 断言:continue 是一个 MatcherContinuation
    3. d 为一个新的 MatcherContinuation,其参数为 (y),不捕获任何内容,并在被调用时执行以下步骤:
      1. 断言:y 是一个 MatchState
      2. 返回 y
    4. rm(matchState, d)。
    5. 如果 rfailure,返回 failure
    6. 断言:r 是一个 MatchState
    7. capr.[[Captures]]
    8. inputmatchState.[[Input]]
    9. xematchState.[[EndIndex]]
    10. zMatchState { [[Input]]: input, [[EndIndex]]: xe, [[Captures]]: cap }。
    11. 返回 continue(z)。
Assertion :: (?<! Disjunction )
  1. m 为以 regexpRecordbackward 为参数的 DisjunctionCompileSubpattern
  2. 返回一个新的 Matcher,其参数为 (matchState, continue),捕获 m,并在被调用时执行以下步骤:
    1. 断言:matchState 是一个 MatchState
    2. 断言:continue 是一个 MatcherContinuation
    3. d 为一个新的 MatcherContinuation,其参数为 (y),不捕获任何内容,并在被调用时执行以下步骤:
      1. 断言:y 是一个 MatchState
      2. 返回 y
    4. rm(matchState, d)。
    5. 如果 r 不是 failure,返回 failure
    6. 返回 continue(matchState)。

22.2.2.4.1 IsWordChar ( regexpRecord, input, e )

The abstract operation IsWordChar takes arguments regexpRecord (一个 RegExp Record,), input (一个字符 List,), and e (一个整数,) and returns 一个 Boolean. It performs the following steps when called:

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

22.2.2.5 Runtime Semantics: CompileQuantifier

The syntax-directed operation CompileQuantifier takes no arguments and returns 一个具有字段 [[Min]](一个非负整数)、[[Max]](一个非负整数或 +∞)和 [[Greedy]](一个 Boolean)的 Record. 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 一个具有字段 [[Min]](一个非负整数)和 [[Max]](一个非负整数或 +∞)的 Record. 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 (一个 RegExp Record,) and direction (forwardbackward,) and returns 一个 Matcher.

Note 1

本节在 B.1.2.7 中被修订。

It is defined piecewise over the following productions:

Atom :: PatternCharacter
  1. ch 为由 PatternCharacter 匹配的字符。
  2. charSet 为一个包含字符 ch 的单元素 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. lm 为一个空的 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 追加到 lm
  7. singles 为包含 cs 中每个由单个字符组成的 CharSetElementCharSet
  8. CharacterSetMatcher(regexpRecord, singles, false, direction) 追加到 lm
  9. 如果 cs 包含空字符序列,将 EmptyMatcher() 追加到 lm
  10. m2lm 中最后一个 Matcher
  11. 对于 lm 的每个 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. 断言:x 是一个 MatchState
    2. 断言:c 是一个 MatcherContinuation
    3. d 为一个新的 MatcherContinuation,其参数为 (y),捕获 xcdirectionparenIndex,并在被调用时执行以下步骤:
      1. 断言:y 是一个 MatchState
      2. capy.[[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. cap[parenIndex + 1] 设置为 r
      9. zMatchState { [[Input]]: input, [[EndIndex]]: ye, [[Captures]]: cap }。
      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. cvCharacterEscapeCharacterValue
  2. ch 为字符值为 cv 的字符。
  3. charSet 为一个包含字符 ch 的单元素 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. lm 为一个空的 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 追加到 lm
  5. singles 为包含 cs 中每个由单个字符组成的 CharSetElementCharSet
  6. CharacterSetMatcher(regexpRecord, singles, false, direction) 追加到 lm
  7. 如果 cs 包含空字符序列,将 EmptyMatcher() 追加到 lm
  8. m2lm 中最后一个 Matcher
  9. 对于 lm 的每个 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 (一个 RegExp Record,), charSet (一个 CharSet,), invert (一个 Boolean,), and direction (forwardbackward,) and returns 一个 Matcher. It performs the following steps when called:

  1. 如果 regexpRecord.[[UnicodeSets]]true,则
    1. 断言:invertfalse
    2. 断言:charSet 的每个 CharSetElement 都由单个字符组成。
  2. 返回一个新的 Matcher,其参数为 (x, c),捕获 regexpRecordcharSetinvertdirection,并在被调用时执行以下步骤:
    1. 断言:x 是一个 MatchState
    2. 断言:c 是一个 MatcherContinuation
    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. ch 为字符 input[index]。
    11. ccCanonicalize(regexpRecord, ch)。
    12. 如果 charSet 中存在一个恰好包含一个字符 aCharSetElement,使得 Canonicalize(regexpRecord, a) 是 cc,则令 foundtrue;否则令 foundfalse
    13. 如果 invertfalsefoundfalse,返回 failure
    14. 如果 inverttruefoundtrue,返回 failure
    15. capx.[[Captures]]
    16. yMatchState { [[Input]]: input, [[EndIndex]]: f, [[Captures]]: cap }。
    17. 返回 c(y)。

22.2.2.7.2 BackreferenceMatcher ( regexpRecord, ns, direction )

The abstract operation BackreferenceMatcher takes arguments regexpRecord (一个 RegExp Record,), ns (一个正整数 List,), and direction (forwardbackward,) and returns 一个 Matcher. It performs the following steps when called:

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

22.2.2.7.3 Canonicalize ( regexpRecord, ch )

The abstract operation Canonicalize takes arguments regexpRecord (一个 RegExp Record,) and ch (一个字符,) and returns 一个字符. It performs the following steps when called:

  1. 如果 HasEitherUnicodeFlag(regexpRecord) 是 trueregexpRecord.[[IgnoreCase]]true,则
    1. 如果 Unicode 字符数据库的文件 CaseFolding.txtch 提供了简单或公共大小写折叠映射,返回将该映射应用于 ch 的结果。
    2. 返回 ch
  2. 如果 regexpRecord.[[IgnoreCase]]false,返回 ch
  3. 断言:ch 是一个 UTF-16 码元。
  4. cp 为其数值为 ch 的数值的码点。
  5. u 为按照 Unicode 默认大小写转换算法得到的 toUppercase(« cp »)。
  6. uStrCodePointsToString(u)。
  7. 如果 uStr 的长度 ≠ 1,返回 ch
  8. cuuStr 的单个码元元素。
  9. 如果 ch 的数值 ≥ 128 且 cu 的数值 < 128,返回 ch
  10. 返回 cu
Note

HasEitherUnicodeFlag(regexpRecord) 是 true 的大小写不敏感匹配中,所有字符在比较之前都会隐式地使用 Unicode 标准提供的简单映射进行大小写折叠。简单映射总是映射到单个码点,因此它不会例如将 ß(U+00DF LATIN SMALL LETTER SHARP S)映射为 ssSS。然而,它可能将基本拉丁块之外的码点映射到其中的码点——例如,ſ(U+017F LATIN SMALL LETTER LONG S)大小写折叠为 s(U+0073 LATIN SMALL LETTER S),而 (U+212A KELVIN SIGN)大小写折叠为 k(U+006B LATIN SMALL LETTER K)。包含这些码点的 String 会被诸如 /[a-z]/ui 的正则表达式匹配。

HasEitherUnicodeFlag(regexpRecord) 是 false 的大小写不敏感匹配中,映射基于 Unicode 默认大小写转换算法 toUppercase,而不是 toCasefold,这会导致一些细微差异。例如,(U+2126 OHM SIGN)通过 toUppercase 映射到自身,但通过 toCasefold 与 Ω(U+03A9 GREEK CAPITAL LETTER OMEGA)一起映射到 ω(U+03C9 GREEK SMALL LETTER OMEGA),因此 "\u2126" 会被 /[ω]/ui/[\u03A9]/ui 匹配,但不会被 /[ω]/i/[\u03A9]/i 匹配。此外,基本拉丁块之外的码点不会被映射到其中的码点,因此诸如 "\u017F ſ""\u212A K" 的 String 不会被 /[a-z]/i 匹配。

22.2.2.7.4 UpdateModifiers ( regexpRecord, add, remove )

The abstract operation UpdateModifiers takes arguments regexpRecord (一个 RegExp Record,), add (一个 String,), and remove (一个 String,) and returns 一个 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 (一个 RegExp Record,) and returns 一个具有字段 [[CharSet]](一个 CharSet)和 [[Invert]](一个 Boolean)的 Record. 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 (一个 RegExp Record,) and returns 一个 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 基本拉丁块中的所有大写和小写字母,以及符号 [, \, ], ^, _`

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. cv 为此 ClassEscapeCharacterValue
  2. c 为字符值为 cv 的字符。
  3. 返回包含单个字符 cCharSet
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. ps 为由 UnicodePropertyName 匹配的源文本
  2. pUnicodeMatchProperty(regexpRecord, ps)。
  3. 断言:pTable 64 的 “Property name and aliases” 列中列出的 Unicode property name 或属性别名。
  4. vs 为由 UnicodePropertyValue 匹配的源文本
  5. vUnicodeMatchPropertyValue(p, vs)。
  6. charSet 为包含所有 Unicode 码点的 CharSet,这些码点的字符数据库定义包含属性 p 且值为 v
  7. 返回 MaybeSimpleCaseFolding(regexpRecord, charSet)。
UnicodePropertyValueExpression :: LoneUnicodePropertyNameOrValue
  1. s 为由 LoneUnicodePropertyNameOrValue 匹配的源文本
  2. 如果 UnicodeMatchPropertyValue(General_Category, s) 是 PropertyValueAliases.txt 中列出的 General_Category (gc) 属性的 Unicode 属性值或属性值别名,则
    1. 返回包含所有 Unicode 码点的 CharSet,这些码点的字符数据库定义包含属性 “General_Category” 且值为 s
  3. pUnicodeMatchProperty(regexpRecord, s)。
  4. 断言:pTable 65 的 “Property name and aliases” 列中列出的二元 Unicode 属性或二元属性别名,或 Table 66 的 “Property name” 列中列出的字符串二元 Unicode 属性。
  5. charSet 为包含所有 CharSetElementCharSet,这些 CharSetElement 的字符数据库定义包含属性 p 且值为 “True”。
  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 中也不是 otherSetCharSetElementCharSetElementCharSet
ClassSubtraction :: ClassSubtraction -- ClassSetOperand
  1. charSet 为以 regexpRecord 为参数的 ClassSubtractionCompileToCharSet
  2. otherSet 为以 regexpRecord 为参数的 ClassSetOperandCompileToCharSet
  3. 返回包含 charSet 中也不是 otherSetCharSetElementCharSetElementCharSet
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. cv 为此 ClassSetCharacterCharacterValue
  2. c 为字符值为 cv 的字符。
  3. 返回包含单个字符 cCharSet
ClassSetCharacter :: \b
  1. 返回包含单个字符 U+0008 (BACKSPACE) 的 CharSet

22.2.2.9.1 CharacterRange ( charSet, otherSet )

The abstract operation CharacterRange takes arguments charSet (一个 CharSet,) and otherSet (一个 CharSet,) and returns 一个 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. 返回包含所有字符值位于从 ij闭区间内的字符的 CharSet

22.2.2.9.2 HasEitherUnicodeFlag ( regexpRecord )

The abstract operation HasEitherUnicodeFlag takes argument regexpRecord (一个 RegExp Record,) and returns 一个 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 (一个 RegExp Record,) and returns 一个 CharSet. 返回一个 CharSet,其中包含出于 \b\B\w\W 的目的而被视为“单词字符”的字符。 It performs the following steps when called:

  1. basicWordChars 为包含 ASCII 单词字符中每个字符的 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 (一个 RegExp Record,) and returns 一个 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 (一个 RegExp Record,) and charSet (一个 CharSet,) and returns 一个 CharSet. 如果 regexpRecord.[[UnicodeSets]]falseregexpRecord.[[IgnoreCase]]false,则返回 charSet。否则,它使用 Unicode 字符数据库文件 CaseFolding.txt 中的 Simple Case Foldingscf(cp))定义(每个定义都将单个码点映射到另一个单个码点),逐字符地将 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 中的每个单个码点 cp,执行
      1. scf(cp) 追加到 t
    3. t 添加到 otherSet
  4. 返回 otherSet

22.2.2.9.6 CharacterComplement ( regexpRecord, complement )

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

  1. charSetAllCharacters(regexpRecord)。
  2. 返回包含 charSet 中也不是 complementCharSetElementCharSetElementCharSet

22.2.2.9.7 UnicodeMatchProperty ( regexpRecord, p )

The abstract operation UnicodeMatchProperty takes arguments regexpRecord (一个 RegExp Record,) and p (ECMAScript 源文本,) and returns 一个 Unicode property name. It performs the following steps when called:

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

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

Note 1

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

Note 2

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

Note 3

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

Table 64: 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 65: 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 66: 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 源文本,) and v (ECMAScript 源文本,) and returns 一个 Unicode 属性值. It performs the following steps when called:

  1. 断言:pTable 64 的 “Canonical property name” 列中列出的规范、无别名 Unicode property name
  2. 断言:vPropertyValueAliases.txt 中列出的 Unicode 属性 p 的属性值或属性值别名。
  3. valuev 的规范属性值,如相应行的 “Canonical property value” 列中所给出。
  4. 返回 Unicode 码点 List value

实现必须支持 PropertyValueAliases.txt 中针对 Table 64 中列出的属性所列出的 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 (一个 RegExp Record,) and returns 一个字符序列. 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 (一个 ECMAScript 语言值,) and flags (一个 String 或 undefined,) and returns 要么是一个包含 Object 的正常完成,要么是一个抛出完成. 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 (一个构造器,) and returns 要么是一个包含 Object 的正常完成,要么是一个抛出完成. 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 (一个 Object,), pattern (一个 ECMAScript 语言值,), and flags (一个 ECMAScript 语言值,) and returns 要么是一个包含 Object 的正常完成,要么是一个抛出完成. 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. 断言:parseResult 是一个 Pattern 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 (一个 Unicode 码点序列,), u (一个 Boolean,), and v (一个 Boolean,) and returns 一个 Parse Node 或一个由 SyntaxError 对象组成的非空 List.

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. patternConstructor 为 ? Get(patternOrRegexp, "constructor")。
      2. 如果 SameValue(newTarget, patternConstructor) 是 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

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

22.2.5 RegExp 构造器的属性

RegExp 构造器

22.2.5.1 RegExp.escape ( str )

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

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

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

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

22.2.5.1.1 EncodeForRegExpEscape ( cp )

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

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

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. str 为 ? ToString(string)。
  4. 返回 ? RegExpBuiltinExec(regexp, str)。

22.2.6.3 get RegExp.prototype.dotAll

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

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

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. 返回其码元为 List codeUnits 元素的 String 值。如果 codeUnits 没有元素,则返回空 String。

22.2.6.4.1 RegExpHasFlag ( regexp, codeUnit )

The abstract operation RegExpHasFlag takes arguments regexp (一个 ECMAScript 语言值,) and codeUnit (一个码元,) and returns 要么是一个包含 Boolean 或 undefined正常完成,要么是一个抛出完成. 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. cu 为码元 0x0067 (LATIN SMALL LETTER G)。
  3. 返回 ? RegExpHasFlag(regexp, cu)。

22.2.6.6 get RegExp.prototype.hasIndices

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

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

22.2.6.7 get RegExp.prototype.ignoreCase

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

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

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

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

  1. regexpthis 值。
  2. 如果 regexp 不是一个 Object,抛出 TypeError 异常。
  3. str 为 ? ToString(string)。
  4. flags 为 ? ToString(? Get(regexp, "flags"))。
  5. 如果 flags 不包含 "g",返回 ? RegExpExec(regexp, str)。
  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, str)。
    2. 如果 resultnull,则
      1. 如果 matchCount = 0,返回 null
      2. 返回 array
    3. matchStr 为 ? ToString(? Get(result, "0"))。
    4. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(matchCount)), matchStr)。
    5. 如果 matchStr 是空 String,则
      1. thisIndex(? ToLength(? Get(regexp, "lastIndex")))。
      2. nextIndexAdvanceStringIndex(str, thisIndex, fullUnicode)。
      3. 执行 ? Set(regexp, "lastIndex", 𝔽(nextIndex), true)。
    6. matchCount 设置为 matchCount + 1。

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

Note

%Symbol.match% 属性由 IsRegExp 抽象操作用于识别具有正则表达式基本行为的对象。不存在 %Symbol.match% 属性,或存在此类属性但其值无法布尔强制转换为 true,表示该对象并非旨在用作正则表达式对象。

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

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

  1. regexpthis 值。
  2. 如果 regexp 不是一个 Object,抛出 TypeError 异常。
  3. str 为 ? ToString(string)。
  4. speciesConstructor 为 ? SpeciesConstructor(regexp, %RegExp%)。
  5. flags 为 ? ToString(? Get(regexp, "flags"))。
  6. matcher 为 ? Construct(speciesConstructor, « 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, str, global, fullUnicode)。

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

22.2.6.10 get RegExp.prototype.multiline

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

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

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

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

  1. regexpthis 值。
  2. 如果 regexp 不是一个 Object,抛出 TypeError 异常。
  3. str 为 ? ToString(string)。
  4. lengthSstr 的长度。
  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, str)。
    2. 如果 resultnull,则
      1. done 设置为 true
    3. 否则,
      1. result 追加到 results
      2. 如果 globalfalse,则
        1. done 设置为 true
      3. 否则,
        1. matchStr 为 ? ToString(? Get(result, "0"))。
        2. 如果 matchStr 是空 String,则
          1. thisIndex(? ToLength(? Get(regexp, "lastIndex")))。
          2. 如果 flags 包含 "u"flags 包含 "v",令 fullUnicodetrue;否则令 fullUnicodefalse
          3. nextIndexAdvanceStringIndex(str, thisIndex, fullUnicode)。
          4. 执行 ? Set(regexp, "lastIndex", 𝔽(nextIndex), true)。
  13. accumulatedResult 为空 String。
  14. nextSourcePosition 为 0。
  15. 对于 results 的每个元素 result,执行
    1. resultLength 为 ? LengthOfArrayLike(result)。
    2. nCapturesmax(resultLength - 1, 0)。
    3. matched 为 ? ToString(? Get(result, "0"))。
    4. matchLengthmatched 的长度。
    5. position 为 ? ToIntegerOrInfinity(? Get(result, "index"))。
    6. position 设置为将 position 夹在 0 和 lengthS 之间的结果。
    7. captures 为一个新的空 List
    8. captureNumber 为 1。
    9. 重复,当 captureNumbernCaptures 时,
      1. capN 为 ? Get(result, ! ToString(𝔽(captureNumber)))。
      2. 如果 capN 不是 undefined,则
        1. capN 设置为 ? ToString(capN)。
      3. capN 追加到 captures
      4. 注:当 captureNumber = 1 时,前一步将第一个元素放入 captures(索引为 0)。更一般地说,第 captureNumberth 个捕获(由第 captureNumberth 组捕获括号捕获的字符)位于 captures[captureNumber - 1]。
      5. captureNumber 设置为 captureNumber + 1。
    10. namedCaptures 为 ? Get(result, "groups")。
    11. 如果 functionalReplacetrue,则
      1. replacerArgs 为 « matched »、captures 和 « 𝔽(position), str » 的列表拼接。
      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, str, position, captures, namedCaptures, replaceValue)。
    13. 如果 positionnextSourcePosition,则
      1. 注:position 通常不应向后移动。如果确实向后移动,则表明存在行为不良的 RegExp 子类,或使用访问触发的副作用来更改 regexp 的 global 标志或其他特性。在此类情况下,相应的替换会被忽略。
      2. accumulatedResult 设置为 accumulatedResultstr 中从 nextSourcePositionposition子字符串以及 replacementString 的字符串拼接。
      3. nextSourcePosition 设置为 position + matchLength
  16. 如果 nextSourcePositionlengthS,返回 accumulatedResult
  17. 返回 accumulatedResultstr 中从 nextSourcePosition 开始的子字符串的字符串拼接。

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

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

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

  1. regexpthis 值。
  2. 如果 regexp 不是一个 Object,抛出 TypeError 异常。
  3. str 为 ? ToString(string)。
  4. previousLastIndex 为 ? Get(regexp, "lastIndex")。
  5. 如果 previousLastIndex 不是 +0𝔽,则
    1. 执行 ? Set(regexp, "lastIndex", +0𝔽, true)。
  6. result 为 ? RegExpExec(regexp, str)。
  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. srcregexp.[[OriginalSource]]
  6. flagsregexp.[[OriginalFlags]]
  7. 返回 EscapeRegExpPattern(src, flags)。

22.2.6.13.1 EscapeRegExpPattern ( pattern, flags )

The abstract operation EscapeRegExpPattern takes arguments pattern (一个 String,) and flags (一个 String,) and returns 一个 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 为形式为 patternSymbol 的一个 String,等价于将 pattern 解释为 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 开头或结尾处的空 substring,也不匹配上一个分隔符匹配结尾处的空 substring。(例如,如果正则表达式匹配空 String,则 String 被分割为单个码元元素;结果数组的长度等于 String 的长度,并且每个 substring 包含一个码元。)即使回溯可以在 String 的给定索引处产生非空 substring 匹配,也只考虑该索引处的第一个匹配。(例如,/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. str 为 ? ToString(string)。
  4. speciesConstructor 为 ? 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(speciesConstructor, « regexp, newFlags »)。
  11. array 为 ! ArrayCreate(0)。
  12. lengthA 为 0。
  13. 如果 limitundefined,令 lim 为 232 - 1;否则令 lim(? ToUint32(limit))。
  14. 如果 lim = 0,返回 array
  15. 如果 str 是空 String,则
    1. matchResult 为 ? RegExpExec(splitter, str)。
    2. 如果 matchResult 不是 null,返回 array
    3. 执行 ! CreateDataPropertyOrThrow(array, "0", str)。
    4. 返回 array
  16. sizestr 的长度。
  17. lastMatchEnd 为 0。
  18. searchIndexlastMatchEnd
  19. 重复,当 searchIndex < size 时,
    1. 执行 ? Set(splitter, "lastIndex", 𝔽(searchIndex), true)。
    2. matchResult 为 ? RegExpExec(splitter, str)。
    3. 如果 matchResultnull,则
      1. searchIndex 设置为 AdvanceStringIndex(str, searchIndex, unicodeMatching)。
    4. 否则,
      1. matchEnd(? ToLength(? Get(splitter, "lastIndex")))。
      2. matchEnd 设置为 min(matchEnd, size)。
      3. 如果 matchEnd = lastMatchEnd,则
        1. searchIndex 设置为 AdvanceStringIndex(str, searchIndex, unicodeMatching)。
      4. 否则,
        1. substringstr 中从 lastMatchEndsearchIndex子字符串
        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. substringstr 中从 lastMatchEndsize子字符串
  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. cu 为码元 0x0079 (LATIN SMALL LETTER Y)。
  3. 返回 ? RegExpHasFlag(regexp, cu)。

22.2.6.16 RegExp.prototype.test ( str )

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

  1. regexpthis 值。
  2. 如果 regexp 不是一个 Object,抛出 TypeError 异常。
  3. string 为 ? ToString(str)。
  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. cu 为码元 0x0075 (LATIN SMALL LETTER U)。
  3. 返回 ? RegExpHasFlag(regexp, cu)。

22.2.6.19 get RegExp.prototype.unicodeSets

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

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

22.2.7 用于 RegExp 匹配的抽象操作

22.2.7.1 RegExpExec ( regexp, str )

The abstract operation RegExpExec takes arguments regexp (一个 Object,) and str (一个 String,) and returns 要么是一个包含 Object 或 null正常完成,要么是一个抛出完成. It performs the following steps when called:

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

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

22.2.7.2 RegExpBuiltinExec ( regexp, str )

The abstract operation RegExpBuiltinExec takes arguments regexp (一个已初始化的 RegExp 实例,) and str (一个 String,) and returns 要么是一个包含 Array 异质对象null正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. lengthstr 的长度。
  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(str);否则令 input 为一个 List,其元素为作为 str 元素的码元。
  12. 注:input 的每个元素都被视为一个字符。
  13. 重复,当 matchSucceededfalse 时,
    1. 如果 lastIndex > length,则
      1. 如果 globaltruestickytrue,则
        1. 执行 ? Set(regexp, "lastIndex", +0𝔽, true)。
      2. 返回 null
    2. inputIndexinput 中从 str 的元素 lastIndex 获得的字符的索引。
    3. rmatcher(input, inputIndex)。
    4. 如果 rfailure,则
      1. 如果 stickytrue,则
        1. 执行 ? Set(regexp, "lastIndex", +0𝔽, true)。
        2. 返回 null
      2. lastIndex 设置为 AdvanceStringIndex(str, lastIndex, fullUnicode)。
    5. 否则,
      1. 断言:r 是一个 MatchState
      2. matchSucceeded 设置为 true
  14. endIndexr.[[EndIndex]]
  15. 如果 fullUnicodetrue,将 endIndex 设置为 GetStringIndex(str, endIndex)。
  16. 如果 globaltruestickytrue,则
    1. 执行 ? Set(regexp, "lastIndex", 𝔽(endIndex), true)。
  17. nr.[[Captures]] 中元素的数量。
  18. 断言:n = regexp.[[RegExpRecord]].[[CapturingGroupsCount]]
  19. 断言:n < 232 - 1。
  20. array 为 ! ArrayCreate(n + 1)。
  21. 断言:array"length" 属性的数学值n + 1。
  22. 执行 ! CreateDataPropertyOrThrow(array, "index", 𝔽(lastIndex))。
  23. 执行 ! CreateDataPropertyOrThrow(array, "input", str)。
  24. matchMatch Record { [[StartIndex]]: lastIndex, [[EndIndex]]: endIndex }。
  25. indices 为一个新的空 List
  26. groupNames 为一个新的空 List
  27. match 追加到 indices
  28. matchedSubstrGetMatchString(str, match)。
  29. 执行 ! CreateDataPropertyOrThrow(array, "0", matchedSubstr)。
  30. 如果 regexp 包含任何 GroupName,则
    1. groupsOrdinaryObjectCreate(null)。
    2. hasGroupstrue
  31. 否则,
    1. groupsundefined
    2. hasGroupsfalse
  32. 执行 ! CreateDataPropertyOrThrow(array, "groups", groups)。
  33. matchedGroupNames 为一个新的空 List
  34. 对于满足 1 ≤ in 的每个整数 i,按升序执行
    1. captureIr.[[Captures]] 的第 ith 个元素。
    2. 如果 captureIundefined,则
      1. capturedValueundefined
      2. undefined 追加到 indices
    3. 否则,
      1. captureStartcaptureI.[[StartIndex]]
      2. captureEndcaptureI.[[EndIndex]]
      3. 如果 fullUnicodetrue,则
        1. captureStart 设置为 GetStringIndex(str, captureStart)。
        2. captureEnd 设置为 GetStringIndex(str, captureEnd)。
      4. captureMatch Record { [[StartIndex]]: captureStart, [[EndIndex]]: captureEnd }。
      5. capturedValueGetMatchString(str, capture)。
      6. capture 追加到 indices
    4. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(i)), capturedValue)。
    5. 如果 regexp 的第 ith 个捕获是用 GroupName 定义的,则
      1. s 为该 GroupNameCapturingGroupName
      2. 如果 matchedGroupNames 包含 s,则
        1. 断言:capturedValueundefined
        2. undefined 追加到 groupNames
      3. 否则,
        1. 如果 capturedValue 不是 undefined,将 s 追加到 matchedGroupNames
        2. 注:如果存在多个名为 s 的组,则 groups 此时可能已经具有一个 s 属性。然而,因为 groups 是一个普通对象,其属性都是可写的数据属性,所以对 CreateDataPropertyOrThrow 的调用仍然保证成功。
        3. 执行 ! CreateDataPropertyOrThrow(groups, s, capturedValue)。
        4. s 追加到 groupNames
    6. 否则,
      1. undefined 追加到 groupNames
  35. 如果 hasIndicestrue,则
    1. indicesArrayMakeMatchIndicesIndexPairArray(str, indices, groupNames, hasGroups)。
    2. 执行 ! CreateDataPropertyOrThrow(array, "indices", indicesArray)。
  36. 返回 array

22.2.7.3 AdvanceStringIndex ( str, index, unicode )

The abstract operation AdvanceStringIndex takes arguments str (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. lengthstr 的长度。
  4. 如果 index + 1 ≥ length,返回 index + 1。
  5. cpCodePointAt(str, index)。
  6. 返回 index + cp.[[CodeUnitCount]]

22.2.7.4 GetStringIndex ( str, codePointIndex )

The abstract operation GetStringIndex takes arguments str (一个 String,) and codePointIndex (一个非负整数,) and returns 一个非负整数. 它按照 6.1.4 中所述,将 str 解释为 UTF-16 编码码点序列,并在存在对应于码点索引 codePointIndex 的码元索引时返回该码元索引。否则,它返回 str 的长度。 It performs the following steps when called:

  1. 如果 str 是空 String,返回 0。
  2. lenstr 的长度。
  3. codeUnitCount 为 0。
  4. codePointCount 为 0。
  5. 重复,当 codeUnitCount < len 时,
    1. 如果 codePointCount = codePointIndex,返回 codeUnitCount
    2. cpCodePointAt(str, codeUnitCount)。
    3. codeUnitCount 设置为 codeUnitCount + cp.[[CodeUnitCount]]
    4. codePointCount 设置为 codePointCount + 1。
  6. 返回 len

22.2.7.5 Match Record

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

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

Table 67: Match Record 字段
字段名 含义
[[StartIndex]] 一个非负整数 从字符串开头到匹配开始处(包含)的码元数量。
[[EndIndex]] 一个整数[[StartIndex]] 从字符串开头到匹配结束处(不包含)的码元数量。

22.2.7.6 GetMatchString ( str, match )

The abstract operation GetMatchString takes arguments str (一个 String,) and match (一个 Match Record,) and returns 一个 String. It performs the following steps when called:

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

22.2.7.7 GetMatchIndexPair ( str, match )

The abstract operation GetMatchIndexPair takes arguments str (一个 String,) and match (一个 Match Record,) and returns 一个 Array. It performs the following steps when called:

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

22.2.7.8 MakeMatchIndicesIndexPairArray ( str, indices, groupNames, hasGroups )

The abstract operation MakeMatchIndicesIndexPairArray takes arguments str (一个 String,), indices (一个由 Match Recordundefined 组成的 List,), groupNames (一个由 String 或 undefined 组成的 List,), and hasGroups (一个 Boolean,) and returns 一个 Array. It performs the following steps when called:

  1. nindices 中元素的数量。
  2. 断言:n < 232 - 1。
  3. 断言:groupNamesn - 1 个元素。
  4. 注:groupNames List 包含从 indices[1] 开始与 indices List 对齐的元素。
  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(str, 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, str, global, fullUnicode )

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

  1. iteratorOrdinaryObjectCreate(%RegExpStringIteratorPrototype%, « [[IteratingRegExp]], [[IteratedString]], [[Global]], [[Unicode]], [[Done]] »)。
  2. iterator.[[IteratingRegExp]] 设置为 regexp
  3. iterator.[[IteratedString]] 设置为 str
  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 对象实例的所有内部槽(见 22.2.9.3),抛出 TypeError 异常。
  4. 如果 iteratorObj.[[Done]]true,则
    1. 返回 CreateIteratorResultObject(undefined, true)。
  5. regexpiteratorObj.[[IteratingRegExp]]
  6. striteratorObj.[[IteratedString]]
  7. globaliteratorObj.[[Global]]
  8. fullUnicodeiteratorObj.[[Unicode]]
  9. match 为 ? RegExpExec(regexp, str)。
  10. 如果 matchnull,则
    1. iteratorObj.[[Done]] 设置为 true
    2. 返回 CreateIteratorResultObject(undefined, true)。
  11. 如果 globalfalse,则
    1. iteratorObj.[[Done]] 设置为 true
    2. 返回 CreateIteratorResultObject(match, false)。
  12. matchStr 为 ? ToString(? Get(match, "0"))。
  13. 如果 matchStr 是空 String,则
    1. thisIndex(? ToLength(? Get(regexp, "lastIndex")))。
    2. nextIndexAdvanceStringIndex(str, 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 68 中列出的内部槽。

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