【发布时间】:2014-09-03 14:48:24
【问题描述】:
我正在尝试使用正则表达式链接主题标签,大多数情况下都有效,除非hot. 末尾有一个带点的单词,这应该只链接#hot,但同时#hot.hot 是有效
这是我的正则表达式代码:
var text = "#hot#hot hot #hot #hot.hot #hót #hot_hot #hot, (#hot) #hot. hot";
text.replace(#([^\b#,() ]*)/g, '<a href="/$1">#$1</a>');
输出:
<a href="/hot">#hot</a><a href="/hot">#hot</a> hot <a href="/hot">#hot</a> <a href="/hot.hot">#hot.hot</a> <a href="/hót">#hót</a> <a href="/hot_hot">#hot_hot</a> <a href="/hot">#hot</a>, (<a href="/hot">#hot</a>) <a href="/hot.">#hot.</a> hot
唯一的问题是#hot. 应该只链接#hot 同时#hot.hot 是有效的
【问题讨论】:
-
我不确定字符类中的“\b”是否真的有任何作用。没有匹配“\b”的字符,因此所有字符都将包含在“[^\b]”中。至少它不处理感叹号之类的东西,如果这是意图的话。
标签: javascript regex hashtag linkify