I need help with an empty string that is always evaluated to true no matter what I do

So, I decided to do a replaceRE and pass the matches to a partial. But I’m facing with a weird behavior, when a group match is empty… is always evaluated to true when I use {{with}}

Does anyone know why this could happen? Since replaceRE is limited on the REPLACEMENT argument, I thought that using a partial would make it easier to insert logic… but I’ve spent a lot of time and I’m out of ideas :sweat_smile:

Any help would be really appreciated.

.RawContent

[[some words|another]]
[[some words]]
[[notes/python/algo]]
[[some words|another]]
[[some words|another]]

single.html

{{ .RawContent | replaceRE `\[\[\s*([^|\]]*)\s*(?:\|\s*([^|\]]*)\s*)?]]` (partial "replaceLink" (dict "Left" "$1" "Right" "$2")) }}

replaceLink.html

{{with .Right}}
    {{ printf "%f" . }}
{{end}}

{{with ""}}
    nothing
{{end}}

Output

%!f(string=another)
%!f(string=)
%!f(string=)
%!f(string=another)
%!f(string=another)

The order of execution the code above is:

  1. (partial "replaceLink" (dict "Left" "$1" "Right" "$2"))
  2. .RawContent | replaceRE \[\[\s*([^|\]]*)\s*(?:\|\s*([^|\]]*)\s*)?]]

No, you are not passing the matched string to the partial.

Ohh, I get it now :thinking: that’s because of the parens that will evaluate the partial first, then it will use that as the replacement.

Then, I guess I just can’t do what I wanted. Thank you :bowing_man:

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