添加标记<a> to message from textarea in python flask

时间:2015-09-11 08:40:02

标签: python flask

How can I add a tag to extracted tags from post from textarea ?

When I add post I have function that split #hastags from post and add to databse and everything works fine.

But I was wondering how I can add tag to every #hastags:

This is my post with #hashtags #sport #music

And I wolud like to add this message like this:

This is my post with <a href="/tag/hashtag">#hashtags</a> <a href="/tag/sport">#sport</a> <a href="/tag/music">#music</a>

1 个答案:

答案 0 :(得分:0)

实现这一目标的一种方法:

import re

text = "This is my post with #hashtags #sport #music"
hashtags = re.findall('#[\w]*', text)

if hashtags:
    a_href = '<a href="/tag/{}">{}</a>'
    for tag in hashtags:
        text = text.replace(tag, a_href.format(tag[1:], tag))

我不知道你对瓶子的期望是什么?如果您在指定链接和格式时需要更多权力,请查看:anchorman