Skip to content

Commit d8495c3

Browse files
committed
Normative: add RegExp.escape (#3382)
1 parent 961f269 commit d8495c3

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

spec.html

+58
Original file line numberDiff line numberDiff line change
@@ -37965,6 +37965,64 @@ <h1>Properties of the RegExp Constructor</h1>
3796537965
<li>has the following properties:</li>
3796637966
</ul>
3796737967

37968+
<emu-clause id="sec-regexp.escape">
37969+
<h1>RegExp.escape ( _S_ )</h1>
37970+
<p>This function returns a copy of _S_ in which characters that are potentially special in a regular expression |Pattern| have been replaced by equivalent escape sequences.</p>
37971+
<p>It performs the following steps when called:</p>
37972+
37973+
<emu-alg>
37974+
1. If _S_ is not a String, throw a *TypeError* exception.
37975+
1. Let _escaped_ be the empty String.
37976+
1. Let _cpList_ be StringToCodePoints(_S_).
37977+
1. For each code point _c_ of _cpList_, do
37978+
1. If _escaped_ is the empty String and _c_ is matched by either |DecimalDigit| or |AsciiLetter|, then
37979+
1. NOTE: Escaping a leading digit ensures that output corresponds with pattern text which may be used after a `\0` character escape or a |DecimalEscape| such as `\1` and still match _S_ rather than be interpreted as an extension of the preceding escape sequence. Escaping a leading ASCII letter does the same for the context after `\c`.
37980+
1. Let _numericValue_ be the numeric value of _c_.
37981+
1. Let _hex_ be Number::toString(𝔽(_numericValue_), 16).
37982+
1. Assert: The length of _hex_ is 2.
37983+
1. Set _escaped_ to the string-concatenation of the code unit 0x005C (REVERSE SOLIDUS), *"x"*, and _hex_.
37984+
1. Else,
37985+
1. Set _escaped_ to the string-concatenation of _escaped_ and EncodeForRegExpEscape(_c_).
37986+
1. Return _escaped_.
37987+
</emu-alg>
37988+
37989+
<emu-note>
37990+
<p>Despite having similar names, EscapeRegExpPattern and `RegExp.escape` do not perform similar actions. The former escapes a pattern for representation as a string, while this function escapes a string for representation inside a pattern.</p>
37991+
</emu-note>
37992+
37993+
<emu-clause id="sec-encodeforregexpescape" type="abstract operation">
37994+
<h1>
37995+
EncodeForRegExpEscape (
37996+
_c_: a code point,
37997+
): a String
37998+
</h1>
37999+
<dl class="header">
38000+
<dt>description</dt>
38001+
<dd>It returns a string representing a |Pattern| for matching _c_. If _c_ is white space or an ASCII punctuator, the returned value is an escape sequence. Otherwise, the returned value is a string representation of _c_ itself.</dd>
38002+
</dl>
38003+
38004+
<emu-alg>
38005+
1. If _c_ is matched by |SyntaxCharacter| or _c_ is U+002F (SOLIDUS), then
38006+
1. Return the string-concatenation of 0x005C (REVERSE SOLIDUS) and UTF16EncodeCodePoint(_c_).
38007+
1. Else if _c_ is the code point listed in some cell of the “Code Point” column of <emu-xref href="#table-controlescape-code-point-values"></emu-xref>, then
38008+
1. Return the string-concatenation of 0x005C (REVERSE SOLIDUS) and the string in the “ControlEscape” column of the row whose “Code Point” column contains _c_.
38009+
1. Let _otherPunctuators_ be the string-concatenation of *",-=&lt;>#&amp;!%:;@~'`"* and the code unit 0x0022 (QUOTATION MARK).
38010+
1. Let _toEscape_ be StringToCodePoints(_otherPunctuators_).
38011+
1. If _toEscape_ contains _c_, _c_ is matched by either |WhiteSpace| or |LineTerminator|, or _c_ has the same numeric value as a leading surrogate or trailing surrogate, then
38012+
1. Let _cNum_ be the numeric value of _c_.
38013+
1. If _cNum_ ≤ 0xFF, then
38014+
1. Let _hex_ be Number::toString(𝔽(_cNum_), 16).
38015+
1. Return the string-concatenation of the code unit 0x005C (REVERSE SOLIDUS), *"x"*, and StringPad(_hex_, 2, *"0"*, ~start~).
38016+
1. Let _escaped_ be the empty String.
38017+
1. Let _codeUnits_ be UTF16EncodeCodePoint(_c_).
38018+
1. For each code unit _cu_ of _codeUnits_, do
38019+
1. Set _escaped_ to the string-concatenation of _escaped_ and UnicodeEscape(_cu_).
38020+
1. Return _escaped_.
38021+
1. Return UTF16EncodeCodePoint(_c_).
38022+
</emu-alg>
38023+
</emu-clause>
38024+
</emu-clause>
38025+
3796838026
<emu-clause id="sec-regexp.prototype">
3796938027
<h1>RegExp.prototype</h1>
3797038028
<p>The initial value of `RegExp.prototype` is the RegExp prototype object.</p>

0 commit comments

Comments
 (0)