嗨,我在 div 中有以下文本:
[http://www.google.com Google this link] some other random text [http://example.com and another website] but don't parse [this is not a link, leave me alone].
我试图做的是将链接转换为普通的 html 链接。格式总是这样,所以它以 [ 开头,然后是 url,然后是链接文本,然后是结束 ]。但我只想匹配链接,而不是方括号中的所有文本。
我想使用 javascript 中的 .match()
函数来完成此任务,但我无法找出正则表达式(我只需要链接的文本部分 -其余的应该是简单的拆分)。
任何帮助将不胜感激。
最佳答案
为什么.match
而不是.replace
?
string.replace(/\[(https?:\/\/[^\]\s]+)(?: ([^\]]*))?\]/g, "<a href='$1'>$2</a>");
关于Javascript 匹配正则表达式链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16144326/