Skip to content

Document that RegExp.prototype[Symbol.replace]() resets lastIndex to 0 before matching - #44957

Open
btea wants to merge 1 commit into
mdn:mainfrom
btea:regexp-symbol-replace-lastindex
Open

Document that RegExp.prototype[Symbol.replace]() resets lastIndex to 0 before matching#44957
btea wants to merge 1 commit into
mdn:mainfrom
btea:regexp-symbol-replace-lastindex

Conversation

@btea

@btea btea commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Description

Note that for global regexes, RegExp.prototype[Symbol.replace]() first sets the regex's lastIndex to 0, so matching always starts from the beginning of the string regardless of the regex's previous lastIndex.

Per ECMA-262 RegExp.prototype [ @@replace ], step 6: if global is true, perform Set(rx, "lastIndex", +0𝔽, true).

Related issues and pull requests

Relates to #44941

@btea
btea requested a review from a team as a code owner July 29, 2026 14:02
@btea
btea requested review from Josh-Cena and removed request for a team July 29, 2026 14:02
@github-actions github-actions Bot added Content:JS JavaScript docs size/xs [PR only] 0-5 LoC changed labels Jul 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:JS JavaScript docs size/xs [PR only] 0-5 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants