XZise added a comment.
Ah! In the first sandbox page is `\0` and `replaceExcept` searches for backslash+number to replace that with the group (like you did with `{{...}}\1` but this time it's part of the text. As far as I can see the first time it works fine and it generates `{{...}}REST OF PAGE` and then (for some reason) it searches in the new result for eventual references again and then finds `\0`. And that code is an infinite loop unless it doesn't find any references again: https://git.wikimedia.org/blob/pywikibot%2Fcore.git/7f50b4ed6bdf27e7e29e6de0...
TASK DETAIL https://phabricator.wikimedia.org/T99032
EMAIL PREFERENCES https://phabricator.wikimedia.org/settings/panel/emailpreferences/
To: XZise Cc: Mpaa, XZise, Aklapper, Arjunaraoc, jayvdb, pywikipedia-bugs