Why does this happen? (runtime error message: "No group 4")

This is wonderful!

You have stumbled upon the very same text replacement bug as another user who tried to use '$' in a text replacement operation.

Two people in a day, after no reports for years.

DO not allow any '$' in your text replacement fragments.
You have one in that global list.