You need to make that match-all non-greedy, or you’re going to consume the entire string for a single match. (Also… if by definition it’s a string with multiple matches, you’re not going to be able to ^ and $… is this a multiline string?)
Well for starters, try using the new pattern rpkamp’s given you.
You can also somewhat simplify your code with a foreach. It’ll help you avoid the other problem you’ve got - putting array markers inside a string doesnt work without help.
If you want to stick with the for loop, and keep the variable inside the string, you’ll need to add some curly braces so the string knows where the variable reference ends, and the string begins again.
It’s been a while since I did complex string insertion, but i believe it would have to be