regex how to replace twitter links


Please help me with regular expression.

I found this good peace of code:

    var ify = function() {
      return {
        "link": function(t) {
          return t.replace(/(^|\s+)(https*\:\/\/\S+[^\.\s+])/g, function(m, m1, link) {
            return m1 + '<a href=' + link + '>' + ((link.length > 25) ? link.substr(0, 24) + '...' : link) + '</a>';
          });
        },
        "at": function(t) {
          return t.replace(/(^|\s+)\@([a-zA-Z0-9_]{1,15})/g, function(m, m1, m2) {
            return m1 + '@<a href="http://twitter.com/' + m2 + '">' + m2 + '</a>';
          });

    },
    "hash": function(t) {
      return t.replace(/(^|\s+)\#([a-zA-Z0-9_]+)/g, function(m, m1, m2) {
        return m

1 + '#<a href="http://search.twitter.com/search?q=%23' + m2 + '">' + m2 + '</a>';
          });
        },
        "clean": function(tweet) {
          return this.hash(this.at(this.link(tweet)));
        }
      };
    }();

But its not working properly.

At first in my page there can be <b>@username</b> and for this cause regex isnt working (i think I need to append this characters "<" and ">" to the "at function". But how?)

At second in "hash" function in my page, in query there can be other non a-zA-Z characters). For example "????? ???????" or "ñ" or others. And formatted string will look like #<a href="twitter.com/?q=Catalu">Catalu</a>ña for #Cataluña word

Please help me. Thank you!

function processTweetLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
    text = text.replace(exp, "<a href='$1' target='_blank'>$1</a>");
    exp = /(^|\s)#(\w+)/g;
    text = text.replace(exp, "$1<a href='http://search.twitter.com/search?q=%23$2' target='_blank'>#$2</a>");
    exp = /(^|\s)@(\w+)/g;
    text = text.replace(exp, "$1<a href='http://www.twitter.com/$2' target='_blank'>@$2</a>");
    return text;
}

Here's my code to do that:

function addTwitterLinks(text) {
    return text.replace(/[\@\#]([a-zA-z0-9_]*)/g,
        function(m,m1) {
            var t = '<a href="http://twitter.com/';
            if(m.charAt(0) == '#')
                t += 'hashtag/';
            return t + encodeURI(m1) + '" target="_blank">' + m + '</a>';
        });
}

And here's a demo of it in action: http://siliconsparrow.com/javascripttwittertest.html


The regex starts with /(^|\s+), this means it matches @foo only when it is at the start of the document or when it is preceded by a space.

Then the regex only matches for letters, numbers and underscores.

Maybe you should make the matching less strict, and match for a series of characters that is not a space, like \@(!\s){1,15}\s, although I'm not sure if those unicode characters are even allowed in Twitter names. Lots of documents mention just [A-Za-z0-9]. Is this changed?