- I created my first web app using Python, Django, Bootstrap and google-app-engine
- The requirement I have is when people suggest external links, The program should be able to find and highlight as clickable URL in the text.
- For example, when we give http://www.google.com in stackoverflow, it converted it as hyperlink
- I have no idea how to achieve this, any help is greatly appreciated

- 87,243
- 191
- 450
- 722
3 Answers
Look into the python re
module.
For example, taking John Gruber's URL regex pattern and matching it against his data set you could do something like...
giant_regex = r'''(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))'''
output_with_links = re.sub(giant_regex, r'<a href="\1">\1</a>', source_html)
Unfortunately this will capture actual links as well that don't need converting, but now your problem is finding the correct regex (which I'm sure has been documented online if you look). The python and django part is done.

- 115,817
- 29
- 282
- 245
This is not a full answer, but will get you started: to do what you want in Django, you will need to (1) take the input text that the user submits, (2) parse it for url patterns, and (3) return the html with a hyperlink to display in the View.
I don't know if there is a canonical regex for this purpose, but some that seem to work well are here and in this answer.
In SO, as you notice, the parsed text is first shown in a separate
display box and, once you hit "submit" is re-rendered. You can choose many ways to render the text (e.g. to parse the text on the client side with Javascript). However, for the first stage you should probably just create a "results" page with each url replaced with a hyperlink (<a href='url'>url</a>
) to that url.