String ending stops replaceRE from working

I have a very simple regex to detect YouTube URL

https?:\/\/youtu\.be\/([a-zA-Z0-9_-]+)

While this works, I want it to end with the string there so I add a $ at the end

https?:\/\/youtu\.be\/([a-zA-Z0-9_-]+)$

And now it isn’t working for the URL https://youtu.be/zp4BMR88260

I tested my expression on regex101 with Golang mode selected and it works there.

Please show how you are using this, not just the expression.

This works:

{{ $content := .Content -}}
{{ $content := $content | replaceRE `https?:\/\/youtu\.be\/([a-zA-Z0-9_-]+)` "https://invidious.osi.kr/watch?v=$1" -}}

This does not work

{{ $content := .Content -}}
{{ $content := $content | replaceRE `https?:\/\/youtu\.be\/([a-zA-Z0-9_-]+)$` "https://invidious.osi.kr/watch?v=$1" -}}
replaceRE `(?m)https?:\/\/youtu\.be\/([a-zA-Z0-9_-]+)$`

But I don’t think you want to do that. It’s looking for a line ending.

Yes, that is my intention, the string of the URL is on its own line

I also tried your new regex but it is still behaving the same way sadly, where it would only work when the $ is removed

Can you share the content file as well?

This will work only when the .Content ends exactly with that youtu.be URL (i.e no spaces, punctuations, etc. allowed).

It would help if you debug print .Content first and see what that looks like.

{{ printf "%#v" .Content }}

Is there a way to process .Content per line? The YouTube urls are scattered throughout the content so it doesn’t end specifically at that url, but they are in their own lines

Again, it will help more if you share your repo or that content .md file.

You don’t want to do it this way. Use a render hook instead.

2 Likes

I cannot upload to the forums so I used a different host
https://file.io/x5HJAklBIrDy or https://file.io/aTW3UAFHNs9A

I will read it, I did not know it existed before

Using @jmooring’s suggestion, link render hooks are perfect for this.

See this commit on my example repo: Add example of link render hook (ecf80851) · Commits · hugo-mwe / Hugo MWE · GitLab. Here is its output: Post with links | Hugo MWE.

I just wanted to add my 2 cent with the notion, that I think the ` around the string lead to the template package of Golang treating that string special (executing it? might not be the right terminology) and all the special characters in that string might have a special meaning in that sense.

replaceRE "https?:\/\/youtu\.be\/([a-zA-Z0-9_-]+)"

might magically work.

When I replace back tick with quote it give this error weirdly

parse failed: template: _default/single.html:2: invalid syntax   

I did not change anything other than the quotes

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