Fix and enhance &Regex.replace/4

This is a PR for #2930.

Fixed the bug and added support for `\g{N}`, but not `\gN` since it wouldn't help when `N` is followed by other digits.