查找并转换所有字符串,以http& https并转换成链接 [英] Find and convert all strings starts with http & https and convert into links

查看:154
本文介绍了查找并转换所有字符串,以http& https并转换成链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以找到所有以http&开头的字符串在段落中使用https并使用jquery转换为链接?

Is it possible to find all strings starts with http & https in a paragraph and convert into links using jquery?

我的网站上有我的twitt列表,所有行均以http& https显示为常规字符串[text].我想将它们转换为超链接.使用javascript,jQuery是否可能.

I got my twitt list in my website and all lines starts with http & https display as normal string [text]. I want to convert them into hyperlinks. Is this possible with javascript, jQuery.

推荐答案

此处是检查URL字符串并将其转换为链接的代码.这也将检测以WWW开头的链接.希望这会有所帮助.

Here is code to check URL's in string and convert it as link. This will detect links which are started with WWW also. Hope this will helps.

function Checkurl(text) {
        var url1 = /(^|<|\s)(www\..+?\..+?)(\s|>|$)/g,
        url2 = /(^|<|\s)(((https?|ftp):\/\/|mailto:).+?)(\s|>|$)/g;

        var html = $.trim(text);
        if (html) {
            html = html
                .replace(url1, '$1<a style="color:blue; text-decoration:underline;" target="_blank"  href="http://$2">$2</a>$3')
                .replace(url2, '$1<a style="color:blue; text-decoration:underline;" target="_blank"  href="$2">$2</a>$5');
        }
        return html;
    }

这篇关于查找并转换所有字符串,以http&amp; https并转换成链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆