From a text created in a <textarea>
, I need to be able to access those links that have been introduced.
For example, I enter:
Lorem ipsum dolor, sit amet consectetur adipisicing elit. https://www.stackoverflow.com Beatae eum nobis distinctiveo sapiente maxime ex dolorum, fuga voluptas architecto
Asperiores voluptatibus cupiditate sequi incident aperiam. https://www.stackoverflow.com Repellat recusandae quas eos error?
(The Stackoverflow editor already detects them automatically, but let's imagine it's plain text)
How could I, from the plain text, change the text link and turn it into a label <a>
to display it in a Django template?
I thought maybe you could search the text for the link with a regex and replace it, but I don't know how to do it. Of course, the regex that I have tried and that works is
regex = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
In principle you can use django.utils.html.urlize or django.utils.html.urlizetrunc for this instead of resorting to implementing it yourself via regex:
To implement something like this with regex , you can make use of
re.sub
enclosing your expression in a group and build the tag from there. For example, using the expression you provide yourself:Extra
If we only want to extract the links, ignoring the rest of the text, we can use
re.finditer
: