我试图在一个元素中获取任何字符串,以启动#
一个虚拟标签系统。基本上
#hello
#goodbye
#good afternoon
然后会显示为
hello
goodbye
good
这是我到目前为止的正则表达式
/#^\s/
我不太擅长正则表达式,但我相信^\s
这是我想要得到的文本,直到空格。
示例 HTML:
<div class="content">
<div>Hello everyone today we are going to be discussing #world_views , <br />
Please enjoy today's discussing by #user2
</div>
</div>
我想要它变成的是
<div class="content">
<div>Hello everyone today we are going to be discussing
<a href="/search&keywords=world%20views">#world_views</a> , <br />
Please enjoy today's discussing by <a href="/search&keywords=user2>#user2</a>
</div>
</div>
到目前为止的完整 JavaScript:
$(function() {
var i,forum={
phpbb2:'.postbody',
phpbb3:'.post .content',
punbb:'.entry-content',
invision:'.postbody'
},
yourVersion="phpbb3";
for (i=0;i<forum[yourVersion].length;i++){
$(forum[yourVersion][i]).html(function() {
return $(this)
.html()
.replace(
/#^\s/,
'<a href="/search&keywords='+$1.replace("#","")+'">$1</a>');
});
}
});