Skip to content Skip to sidebar Skip to footer

Find And Convert All Strings Starts With Http & Https And Convert Into Links

Is it possible to find all strings starts with http & https in a paragraph and convert into links using jquery? I got my twitt list in my website and all lines starts with http

Solution 1:

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;
    }

Solution 2:

Something like this?

$(document).ready(function() {
   var linkable_content = $('div#linkable').html().replace(/(https?:\/\/[^ ;|\\*'"!,()<>]+\/?)/g,'<a href="$1">$1</a>');
    $('div#linkable').html(linkable_content);
});

Source: http://tangible.ca/articles/44/snippet-jquery-linkifier

Solution 3:

The following utility function from the jQuery WikiText Plugin will work:

functionfindUrls( text )
{
    var source = (text || '').toString();
    var urlArray = [];
    var url;
    var matchArray;

    // Regular expression to find FTP, HTTP(S) and email URLs.var regexToken = /(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w-]+@([\w][\w\-]+\.)+[a-zA-Z]{2,3})/g;

    // Iterate through any URLs in the text.while( (matchArray = regexToken.exec( source )) !== null )
    {
        var token = matchArray[0];
        urlArray.push( token );
    }

    return urlArray;
}

JSFiddle Here.

I hope this helps!

Post a Comment for "Find And Convert All Strings Starts With Http & Https And Convert Into Links"