How to urlize text correctly that has a "+"?

Silly question, but I’m a bit stuck on this.

I have several texts that I need to convert to links. I do this by using the urlize function which works.

The issue comes when there’s a “+” in the text. The urlize function converts the plus into “–” instead of “-”. I assume this is normal behavior. How would I go about this if I just want “+” to be replaced with “-”?

Is there a better way than using a replace statement?

Example:

myText = “This is my text”

{{.Params.myText | urlize}} --> this-is-my-text

myText = “This is my text + something”

{{.Params.myText | urlize}} --> this-is-my-text--something

The behavior I want:

{{.Params.myText | urlize}} --> this-is-my-text-something

Silly trick:

{{ $myText := "The !@$ quick %^& brown *() fox -_+ jumps =[] over {}| :;' the <>? lazy + dog" }}
{{ $myText | humanize | urlize }}

Output:

the-quick-brown-fox-jumps-over-the-lazy-dog

Of the special characters on the US keyboard, this will handle everything except #, \, and .

A more complete solution is:

{{ $myText | replaceRE `[~#_+.\\/]` `-` | urlize }}
1 Like

Excellent. Thank you! :slight_smile:

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