Find last part of split

Hi all!

The title is not optimal. What I am trying is this:

Replace an image path, say

/images/2017/08/…/topic/my-image.jpg

through

/images/2017/08/…/topic/THUMBNAIL/my-image.jpg

It would make sense to use something like {{ $image := split $Params.my_image "/" }}.

But how is it possible to insert something (like the “THUMBNAIL” folder name) before the last element?

Thank you, guys!

I think what you want is replaceRE:

In case you’re not familiar with regular expressions:

{{ replaceRE "/([^/]+)$" "/THUMBNAIL/$1" "/images/2017/08/…/topic/my-image.jpg" }}

should do the trick.

1 Like

Thank you very, very much. Your answers saved me ages. (I have worked with regular expressions apart from Hugo and hated it :wink: )

If you ever need to try out your Regex, you can use https://regex101.com/ which allows you to switch to Golang flavor, which is what Hugo uses.

1 Like

Thank you very much, @budparr. That is a very tool, indeed!