String filter with reg expression


I have got within my markdown files an page item

id: "hello-123 any new word with any character !?"

It can be any string, but for my page I need a string only with the regular expression [a-zA-Z0-9_]
How can I filter a string on a Hugo template / partial so that it contains only the elements within the regular expression?



You could invert your regular expression and delete all characters that don’t match, i.e. the replaceRE template would replace each match with an empty string. Hence the final result only contains valid chars.

{{ "hello-123...character !?" | replaceRE regexp "" }}

I found a solution with

replaceRE "[[:^alpha:]]" "" "<here my string>"

My mistake was the ^ before the [, but it must be within the []

1 Like