Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 827
pleerock Avatar asked Nov 05 '11 14:11

pleerock


People also ask

Can you use regex in Twitter search?

Twitter unfortunately doesn't support searching of tweets using regular expressions which means that you do have to post process. There's not actually any official documentation from Twitter to that effect, but everyone who uses the Twitter search API post-processes their tweets using regex (including me).

How do you format a link on twitter?

Type or paste the URL into the Tweet box on twitter.com. A URL of any length will be altered to 23 characters, even if the link itself is less than 23 characters long. Your character count will reflect this. Click the Tweet button to post your Tweet and link.


2 Answers

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;
}
like image 73
Mohamed Wasfi Avatar answered Oct 29 '22 10:10

Mohamed Wasfi


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

like image 44
Adam Pierce Avatar answered Oct 29 '22 11:10

Adam Pierce