在字符串中检测链接并在Javascript中插入锚标记

在字符串中检测链接并在Javascript中插入锚标记,javascript,Javascript,我有这样一个字符串: let string = "This is some text, this [should be a link](https://www.google.com.ar)"; 像markdown,但我不想使用整个markdown库,因为链接是我唯一应该查找的东西 上面的字符串应生成以下内容: "This is some text, this <a href="https://www.google.com.ar" target="_blank">should be

我有这样一个字符串:

let string = "This is some text, this [should be a link](https://www.google.com.ar)";
像markdown,但我不想使用整个markdown库,因为链接是我唯一应该查找的东西

上面的字符串应生成以下内容:

"This is some text, this <a href="https://www.google.com.ar" target="_blank">should be a link</a>"
“这是一些文本,这是”

编辑:更准确地说,我不想使用标记分析,因为我不想替换所有其他标记内容,只替换链接。

您可以使用RegExp将文本替换为锚定标记,如下所示:

let string = "This is some text, this [should be a link](https://www.google.com.ar)";
let string=“这是一些文本,这[应该是一个链接](https://www.google.com.ar)";
控制台日志(
string.replace(/\[.*?\]\(.*?\)/g,text=>{
让[fullmatch,name,link]=/\[(.*?\]\(.*?)/g.exec(文本)
返回``
})      

)
你可以在谷歌上搜索“regexp backreference”,这将对你有所帮助:-)有人已经发布了答案,但还是要谢谢!!:)