解析主题标签的文本并使用JavaScript替换链接

时间:2015-10-14 18:27:05

标签: javascript regex

如何用JavaScript替换twitter风格的#hashtags?

即。 <android.support.v7.widget.RecyclerView android:id="@+id/rv_fragment_dashboardhome" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </android.support.v7.widget.RecyclerView> 应该成为this is a #hashtag

对于PHP:Parse text for hashtags and replace with links using php

1 个答案:

答案 0 :(得分:3)

t = 'this is a #hashtag'
t.replace(/#(\w+)/g, '<a href="tag/$1">#$1</a>')

给出

this is a <a href="tag/hashtag">#hashtag</a>