3

See line 8 in the code below, with the comment:

<script>
$(function(){
    $.getJSON('http://twitter.com/status/user_timeline/TWITTER.json?count=1&callback=?',twitterJSON);
    function twitterJSON(data){
        var twitterOut     = '<p>'+data[0].text+'</p><strong>- '+data[0].user.name+'</strong>';
        var twitterOutAt   = twitterOut.replace(/\B@([\w-]+)/gm,'<a href="http://twitter.com/$1">@$1</a>');
        var twitterOutHash = twitterOutAt.replace(/\B#([\w-]+)/gm,'<a href="http://search.twitter.com/search?q=$1">#$1</a>');
        var twitterOutDone = twitterOutHash.replace(/(href="|<a.*?>)?[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g,'<a href="$1">$1</a>'); // not working :(
        $('.twitter').html(twitterOutDone);
    }
});
</script>

Any help refactoring the code would be very much appreciated!

For example: there must be a way to chain .replace so one doesn't have to assign a new var again and again. I've tried var twitterOut.replace().replace()... but that doesn't seem to work :(

1
  • 2
    Leaving the rest aside, you can certainly chain replaces (as a replace returns the new string on which you can apply another replace), could you post the code that's not working for you? Commented Dec 2, 2011 at 8:15

2 Answers 2

6

Here is the working function I use:

jQuery.fn.urlize = function() {
    if (this.length > 0) {
        this.each(function(i, obj){
            // making links active
            var x = $(obj).html();
            var list = x.match( /\b(http:\/\/|www\.|http:\/\/www\.)[^ <]{2,200}\b/g );
            if (list) {
                for ( i = 0; i < list.length; i++ ) {
                    var prot = list[i].indexOf('http://') === 0 || list[i].indexOf('https://') === 0 ? '' : 'http://';
                    x = x.replace( list[i], "<a target='_blank' href='" + prot + list[i] + "'>"+ list[i] + "</a>" );
                }

            }
            $(obj).html(x);
        });
    }
};

In your case:

$('.twitter').urlize();
Sign up to request clarification or add additional context in comments.

Comments

2

I found a solution here: https://stackoverflow.com/a/3890175/1076933

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Twitter</title>
</head>
<body id="top">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>

    $(function() {

        $.getJSON("http://twitter.com/status/user_timeline/envato.json?count=1&callback=?", twitterJSON);

        function twitterJSON(data) {
            var twitterOut = data[0].text;
            twitterOut = twitterOut
                .replace(/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim, '<a href="$1">$1</a>')
                .replace(/\B#([\w-]+)/gm, '<a href="http://search.twitter.com/search?q=$1">#$1</a>')
                .replace(/\B@([\w-]+)/gm, '<a href="http://twitter.com/$1">@$1</a>');
            $("body").html(twitterOut);
        };

    });

</script>
</body>
</html>

Thanks to Silver Light as well, his function works a charm!

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Twitter</title>
</head>
<body id="top">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>

    $(function() {

        jQuery.fn.urlize = function() {
            if (this.length > 0) {
                this.each(function(i, obj) {
                    var x = $(obj).html();
                    var list = x.match(/\b(http:\/\/|www\.|http:\/\/www\.)[^ <]{2,200}\b/g);
                    if (list) {
                        for (i = 0; i < list.length; i++) {
                            var prot = list[i].indexOf("http://") === 0 || list[i].indexOf("https://") === 0 ? "" : "http://";
                            x = x.replace(list[i], "<a target='_blank' href='" + prot + list[i] + "'>" + list[i] + "</a>");
                        }
                    }
                    $(obj).html(x);
                })
            }
        };

        $.getJSON("http://twitter.com/status/user_timeline/envato.json?count=1&callback=?", twitterJSON);

        function twitterJSON(data) {
            var twitterOut = data[0].text;
            twitterOut = twitterOut
                .replace(/\B#([\w-]+)/gm, '<a href="http://search.twitter.com/search?q=$1">#$1</a>')
                .replace(/\B@([\w-]+)/gm, '<a href="http://twitter.com/$1">@$1</a>');
            $("body").html(twitterOut).urlize();
        };

    });

</script>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.