zl程序教程

您现在的位置是:首页 >  前端

当前栏目

将一个符合URL格式的字符串变成链接

链接 一个 字符串 格式 url 变成 符合
2023-09-11 14:17:02 时间
function replaceURLWithHTMLLinks(text) {
      /*
        Example:

            >>> GateOne.Utils.replaceURLWithHTMLLinks('Downloading http://foo.bar.com/some/file.zip');
            "Downloading <a href="http://foo.bar.com/some/file.zip">http://foo.bar.com/some/file.zip</a>"
        */
        var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
        return text.replace(exp,"<a href="+$1+">$1</a>");
    }

原:http://www.cnblogs.com/rubylouvre/p/3480013.html