Performing functions on a captured group in replaceRE

I think it’s not possible to do what you want in exactly the same manner. But you can “extract” each subgroup into a different var and work on that … (below is not tested … but you will get the idea):

{{ $subgroup1 := .Content | replaceRE "(?U)<h2>(.*)</h2>" "${1}" }}
{{ .Content | replaceRE "(?U)<h2>(.*)</h2>" (printf "<h2 id='%s'>%s</h2>" (anchorize $subgroup1) $1) | safeHTML }}

It’s not efficient, but it seems to be at least doable.


I am actually doing something like that in one of my themes:

{{ $hugo_version_string_1 := hugo.Version }}
{{ with hugo.CommitHash }} <!-- Example value: "975bb629:chroma-d5ca12b" -->
    {{ $hugo_commit_string := . | replaceRE "^([0-9a-f]{7,}).*" "${1}" }}
    {{ $hugo_version_string_1 = printf `<a href="%s/commit/%s">%s</a>` $site_params.urls.hugo $hugo_commit_string $hugo_version_string_1 }}
{{ end }}
{{ $hugo_version_string := printf `<span class="nobr">Hugo %s</span>` $hugo_version_string_1 }}