Spec issues · defect of the text · docs/spec-issues/11-regexp-lastindex-inside-surrogate-pair.md
11. /u with lastIndex inside a surrogate pair: a match reported one code unit late, and a Match Record that ends before it starts
- Location
- §22.2.7.2 RegExpBuiltinExec (
#sec-regexpbuiltinexec), steps 13.b, 15, 22, 24 and 28; §22.2.7.5 Match Records (#sec-match-records); §22.2.7.6 GetMatchString (#sec-getmatchstring), step 1 (also §22.2.7.7 GetMatchIndexPair (#sec-getmatchindexpair), step 1). - Kind
- a result that every engine tried contradicts (and on which they disagree), and, for an empty match, an ill-formed Match Record and a failing Assert.
- Model erratum
GetMatchStringStartAfterEnd, default reading literal; its readings: literal, proposed (the proposed reading moves lastIndex back to the start of the code point, as under Proposed wording); the literal reading stops at GetMatchString step 1 under RegExpBuiltinExec. The model reads step 13.b's character as the code point that contains the code unit at lastIndex.- Upstream
- Checked 2026-11-19: issue #128 (open, 2015-10), "Unicode RegExp with index points trail surrogate in surrogate pair is not covered in the spec", and issue #2511 (open, 2021-09), "Underspecified behavior of global unicode regular expressions with lastIndex inside a surrogate pair", ask what the index of step 13.b means inside a pair; on #2511 (2021-09-11) the author of that step's wording answered that it refers to the code point of the pair, and #2511 was called a duplicate of #128.
- On this site
GetMatchStringStartAfterEndon the Errata page (defect of the text; its default: the literal text)
Text
RegExpBuiltinExec:
13. Repeat, while matchSucceeded is false,
b. Let inputIndex be the index into input of the character that was obtained from element lastIndex of string.
c. Let result be matcher(input, inputIndex).
14. Let endIndex be result.[[EndIndex]].
15. If fullUnicode is true, set endIndex to GetStringIndex(string, endIndex).
16. If global is true or sticky is true, then
a. Perform ? Set(regexp, "lastIndex", 𝔽(endIndex), true).
…
22. Perform ! CreateDataPropertyOrThrow(array, "index", 𝔽(lastIndex)).
…
24. Let match be the Match Record { [[StartIndex]]: lastIndex, [[EndIndex]]: endIndex }.
…
28. Let matchedSubstring be GetMatchString(string, match).
The Match Record table gives [[EndIndex]] the value "an integer ≥ [[StartIndex]]". GetMatchString: "1. Assert: match.[[StartIndex]] ≤ match.[[EndIndex]] ≤ the length of string."
Problem
With the u or v flag, input is the String's code points. When lastIndex is the index of the trailing surrogate of a pair, step 13.b gives the code point that contains it, which begins one code unit earlier. Nothing moves lastIndex back with it, and steps 22 and 24 report the match from lastIndex:
- A non-empty match.
/./guwithlastIndex = 1on"\u{1F600}x"matches the code point U+1F600, so endIndex is 2. Step 22 reportsindex1, and step 28's matchedSubstring is the substring from 1 to 2, the lone trailing surrogate 0xDE00. No Assert is involved; every engine tried contradicts this result. - An empty match. If the pattern matches the empty String there, endIndex is the start of that code point, lastIndex - 1, and step 24 builds the Match Record { [[StartIndex]]: lastIndex, [[EndIndex]]: lastIndex - 1 }, which the Match Record table does not allow. Step 28's GetMatchString Assert fails (with the d flag, GetMatchIndexPair's too).
Examples
examples/11-regexp-lastindex-inside-surrogate-pair.js (outputs: the recorded runs):
var n = /./gu;
n.lastIndex = 1;
var mn = n.exec("\u{1F600}x");
print("non-empty: index " + mn.index + ", match length " + mn[0].length +
", first unit " + mn[0].charCodeAt(0).toString(16) + ", lastIndex " + n.lastIndex);
var r = /(?:)/gu;
r.lastIndex = 1;
var m = r.exec("\u{1F600}");
print("empty: index " + m.index + ", match length " + m[0].length + ", lastIndex " + r.lastIndex);
var rd = /(?:)/gud;
rd.lastIndex = 1;
var md = rd.exec("\u{1F600}");
print("indices[0] " + JSON.stringify(md.indices[0]));
| non-empty | empty | indices[0] |
|
|---|---|---|---|
| literal text | index 1, match length 1, first unit de00, lastIndex 2 |
GetMatchString's Assert fails ({ [[StartIndex]]: 1, [[EndIndex]]: 0 }) | (not reached) |
| V8, SpiderMonkey | index 0, match length 2, first unit d83d, lastIndex 2 |
index 0, match length 0, lastIndex 0 |
[0,0] |
| JavaScriptCore | index 2, match length 1, first unit 78, lastIndex 3 |
index 1, match length 0, lastIndex 1 |
[1,1] |
| formal model (literal reading) | (not printed) | stops at GetMatchString step 1 | (not printed) |
| formal model (proposed reading) | index 0, match length 2, first unit d83d, lastIndex 2 |
index 0, match length 0, lastIndex 0 |
[0,0] |
V8 and SpiderMonkey start the match, and report it, at the start of the code point that contains lastIndex (index
0). JavaScriptCore does not match the code point that begins before lastIndex: its non-empty match is the next code
point (x, at index 2), and its empty match lies at index 1, between the two code units of the pair, a position that
the code-point input does not have. No engine produces the literal result. The model's literal reading stops at
GetMatchString step 1 (stop: erratum GetMatchStringStartAfterEnd sec-getmatchstring#0 1), and a run that stops
reports only the stop; under the proposed reading (--erratum GetMatchStringStartAfterEnd=proposed) it prints V8's
and SpiderMonkey's lines.
Proposed wording
Proposed: start the match, and report it, at the first code unit of the code point, as V8 and SpiderMonkey do. This fixes both cases. (The other repair, moving lastIndex forward to the next code-point boundary, is JavaScriptCore's non-empty result; its empty match would then be at index 2.)
--- a/spec.html
+++ b/spec.html
@@ -39823,6 +39823,7 @@
1. Perform ? <emu-meta suppress-effects="user-code">Set(_regexp_, *"lastIndex"*, *+0*<sub>𝔽</sub>, *true*)</emu-meta>.
1. Return *null*.
1. Let _inputIndex_ be the index into _input_ of the character that was obtained from element _lastIndex_ of _string_.
+ 1. If _fullUnicode_ is *true*, set _lastIndex_ to GetStringIndex(_string_, _inputIndex_).
1. Let _result_ be _matcher_(_input_, _inputIndex_).
1. If _result_ is <span class="const">failure</span>, then
1. If _sticky_ is *true*, then
test262
No test sets lastIndex inside a surrogate pair before a /u match. built-ins/RegExp/prototype/exec/u-lastindex-adv.js
and built-ins/RegExp/prototype/Symbol.split/u-lastindex-adv-thru-*.js advance through pairs with
AdvanceStringIndex, which never stops inside one.
The program
The example is a Script. Its runs on the engines and on the model: the examples' runs.
Open in the debugger: The specification Open in the debugger: The specification, with GetMatchStringStartAfterEnd read as proposed reading
Source examples/11-regexp-lastindex-inside-surrogate-pair.js
// RegExpBuiltinExec with /u and lastIndex on the trailing surrogate of a pair.
// A non-empty match (steps 13.b, 22, 24, 28): the literal text reports index 1
// and a lone trailing surrogate as the match.
// An empty match: match = { [[StartIndex]]: 1, [[EndIndex]]: 0 }, which the
// Match Record table does not allow, and GetMatchString step 1's Assert fails.
if (typeof print === "undefined") globalThis.print = console.log;
var n = /./gu;
n.lastIndex = 1;
var mn = n.exec("\u{1F600}x");
print("non-empty: index " + mn.index + ", match length " + mn[0].length +
", first unit " + mn[0].charCodeAt(0).toString(16) + ", lastIndex " + n.lastIndex);
var r = /(?:)/gu;
r.lastIndex = 1;
var m = r.exec("\u{1F600}");
print("empty: index " + m.index + ", match length " + m[0].length + ", lastIndex " + r.lastIndex);
var rd = /(?:)/gud;
rd.lastIndex = 1;
var md = rd.exec("\u{1F600}");
print("indices[0] " + JSON.stringify(md.indices[0]));