使用#符号将文本转换为javascript中的链接

Mr *_*nes 2 javascript regex twitter

我想知道如何使用Javascript执行以下操作.

假设我有如下文字

var test = '#test is a #cool #place';
Run Code Online (Sandbox Code Playgroud)

我想要以下内容

<a href="abc.html?q=test">#test</a> is a <a href="abc.html?q=cool">#cool</a> <a href="abc.html?q=place">#place</a>
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

geo*_*org 5

试试replace():

var test = '#test is a #cool #place';
var html = test.replace(/#(\w+)/g, '<a href="abc.html?q=$1">#$1</a>')
Run Code Online (Sandbox Code Playgroud)