Superscript with replaceRE

I thought I should ask this here since I cannot find any solution online. I want to use replaceRE to make all instances of ^st, ^nd, ^rd and ^th in my content to be converted to <sup>content here</sup>. But I am not sure how to write such a regular expression. Anyone who can help me out? (The closest I have come is to using this solution but I want to avoid using the { } symbols.)

Could you please provide an example?

In my content, I have some numbers as 30^th, 3^rd, 21^st, 2^nd, etc. I found out CommonMark does not support these symbols. So, I want to make sure Hugo does, and replaceRE is the closest I have seen to achieving that.

replaceRE "\^(rd|th|st|nd)" "<sup>$1</sup>" .Content
or so

1 Like

I think @chrillek’s solution meet your requirement, you can also apply it on numbers only.

{{ $pattern := `(\d+)\^(st|nd|rd|th)` }}
{{ $replacement := "$1<sup>$2</sup>" }}
{{ replaceRE $pattern $replacement "1^st 2^nd 3^rd 4^th foo^st bar^nd" | safeHTML }}

image

Thanks y’all. I appreciate. This worked.

    {{- .Content 
        | replaceRE `(\d+)\^(st|nd|rd|th)` "$1<sup>$2</sup>"
        | safeHTML  
    }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.