Thanks for the replies.
You’re right, @kaushalmodi, BlackFriday will indeed generate the ids for me automatically. I was trying to do this myself as I was confused at the time as I shot myself in the foot with a custom shortcode. I realize now that I don’t get the benefits of BlackFriday while in a shortcode. It makes perfect sense now that @maiki helped me through it.
With that said, I’m still curious if it’s possible to do what I was trying to do. The regex works fine and I’m able to print the captured group perfectly fine. What I’m not sure is feasible or not is to perform some functions on $1 or if I’m limited to straight up insertion into the replacement string. I played with it some more and thought maybe something like this would work:
{{ .Content | replaceRE "(?U)<h2>(.*)</h2>" (printf "<h2 id='%s'>%s</h2>" (anchorize $1) $1) | safeHTML }}
But $1 is not defined in this context. I’m guessing it’s only known to replaceRE itself and therefore it’s simply not possible.
It’s just a curiosity really so no big deal if I can’t. I’m mainly just interested to see what can and cannot be done in Hugo templates.