Skip to content

Instantly share code, notes, and snippets.

@iainireland
iainireland / regexp-builtin-exec.txt
Created March 2, 2021 00:40
RegExpBuiltinExec algorithm, renumbered
# 1.1.4.1.1 Runtime Semantics: RegExpBuiltinExec ( R, S )
The abstract operation RegExpBuiltinExec with arguments R and S performs the following steps:
1. Assert: _R_ is an initialized RegExp instance.
2. Assert: Type(_S_) is String.
3. Let _length_ be the number of code units in _S_.
4. Let _lastIndex_ be ? ToLength(? Get(_R_, `"lastIndex"`)).
5. Let _flags_ be _R_.[[OriginalFlags]].
6. If _flags_ contains `"g"`, let _global_ be *true*, else let _global_ be *false*.
As of Firefox 78, we will ship an updated version of the regexp engine, which will support dotAll, unicode property escapes, and lookbehind assertions.
Use case:
The dotAll flag allows `.` in regular expressions to match any character, including newlines.
Unicode property escapes can be used to match characters according to certain Unicode properties. For example, `/^\p{Decimal_Number}+$/u.test('𝟏𝟐𝟑𝟜𝟝𝟞𝟩𝟪𝟫𝟬𝟭𝟮𝟯𝟺𝟻𝟼') === true`.
Lookbehind assertions can be used to make sure that a pattern is or is not preceded by another (just as lookahead assertions can be used to make that a pattern is or is not succeeded by another).
// Not-very-compliant polyfill for setTimeout, based on Promise.
// When this array becomes non-empty, call scheduleWork.
let timeouts = [];
// Fake clock used to order timeouts.
let now = 0;
// Least positive integer not yet used as a timer id.
let next_id = 1;