2

嗨,我在 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,然后是链接文本,然后是结束 ]。但我只想匹配链接,而不是方括号中的所有文本

我想.match()在 javascript 中使用该函数来完成此任务,但我无法弄清楚正则表达式(我只需要作为链接的文本部分 - 其余部分应该是一个简单的拆分)。

任何帮助将不胜感激。

4

1 回答 1

5

Why .match and not .replace?

string.replace(/\[(https?:\/\/[^\]\s]+)(?: ([^\]]*))?\]/g, "<a href='$1'>$2</a>");
于 2013-04-22T10:07:00.697 回答