Find last part of split


#1

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!


#2

I think what you want is replaceRE:


#3

In case you’re not familiar with regular expressions:

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

should do the trick.


#4

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


#5

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.


#6

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