I am trying to translate this:
{% img <right> /images/testing %}
into this:
{{< figure <class="right"> src="/images/testing" >}}
with regex in Golang. The part in <>
in the source string is optional.
I have this code, which seems to work in the main test case, when the first capturing group exists ("right"):
regexp.MustCompile(`{%\s*img\s*(\p{L}*)\s+([/\S]+)\s+%}`)
.ReplaceAllString("{% img right /images/testing %}", "{{< figure class=\"$1\" src=\"$2\" >}}")
If the optional group is missing, however, I get:
{{< figure class="" src="/images/testing" >}}
which is not what I need - I want the entire class=""
section gone, like this:
{{< figure src="/images/testing" >}}
Is this possible? Can I indicate somehow in the replacing string:
{{< figure class=\"$1\" src=\"$2\" >}}
that I want the additional text ("class=") gone if the optional group is empty?