String.replace with anonymous function with multiple matches

String.replace with a Regex uses Regex.replace under the hood, but the declaration of String.replace specifically narrows the allowed type of the function. Looks like it’s been that way since the feature was added:

https://github.com/elixir-lang/elixir/pull/9073

Using Regex.replace directly should do what you want.