The replacement is verbatim $1 with no upper applied to it, as I’d expect.
If I change $1 to a regular string, the replacement does work however, e.g.: replaceRE "_([a-z])" ("test" | upper) $key // 'exampleTESTield' replaceRE "_([a-z])" (upper "test") $key // 'exampleTESTield'
I’m not sure how $1 behaves under-the-hood, but I don’t know how I can use it with a filter function, or how I can use replaceRE to perform more advanced replacements than just returning groups.
Thanks @alexandros I did try that approach but Hugo fails the build as it’s an undefined variable, which makes sense as replaceRE doesn’t expose it as a local variable (much like PHP where group matched variables like $1, $2 etc aren’t exposed as variables, but only used in replacement).
I did read the rules on a sample repository but as this is a one-line issue with no context required beyond replaceRE I didn’t spin up a repo (and I can’t share the one I’m currently working on).
I’m really not sure how to make this work as calling upper or a function on "$1" is what will be passed to replaceRE. Only alternative I can think of is to split on underscores…
Thanks @maiki, unfortunately I cannot change the source in this instance. Particularly because of how Hugo / Viper handles parameters in YAML config. This function runs in a partial template, I’m not sure where else to put this “logic”, as much as I’d rather avoid lots of logic in the template.
I need the keys to be camelCased, but defining the keys in YAML as camelCase is not effective as once the parameters are available to the template, they have been lowercased. So to work around this, we’ve made the keys snake_cased, and then in the template they’re converted to camelCase – this is a requirement to pass some variables to a third party JS library.
replaceRE uses regexp.ReplaceAllString in the background, so it won’t allow what you’re wanting (as you’ve seen). I doubt we want to add a framework for allowing regexp.ReplaceAllStringFunc from the templates, which could allow what you’re trying to do.
Having this feature will add a lot of power (over the one we already have ) to the templates. Is it too complicated to enable what the OP wants to do? Would it be possible to make replaceRE’s returned regex subgroups to be operable by other template funcs?