How to parse URLs, hash tags, and more from a tweet

ethanm
ethanm
8 Points
4 Posts

I'm using twitter api to get recent tweet by hashtag. I want to parse the tweet text as appears in the twitter site. How we can do this?

Views: 8149
Total Answered: 3
Total Marked As Answer: 0
Posted On: 07-Sep-2017 04:41

Share:   fb twitter linkedin
Answers
Brian
Brian
Participant
536 Points
73 Posts
     

Try following methods:

String.prototype.parseTwitterURL = function () {
    return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+.[A-Za-z0-9-_:%&~?/.=]+/g, function (url) {
        return url.link(url);
    });
};
String.prototype.parseTwitterUsername = function () {
    return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
        var username = u.replace("@","")
        return u.link("https://twitter.com/"+username);
    });
};
String.prototype.parseTwitterHashtag = function () {
    return this.replace(/[#]+[A-Za-z0-9-_]+/g, function (t) {
        var tag = t.replace("#", "")
        return t.link("https://twitter.com/hashtag/" + tag);
    });
};

Use it as:

var parsedTweets = tweet.parseTwitterURL().parseTwitterUsername().parseTwitterHashtag()
Posted On: 11-Sep-2017 01:50
ethanm
ethanm
8 Points
4 Posts
     

Thanks Brian,

Can we do this with instagram, also?

Posted On: 13-Sep-2017 04:45
Brian
Brian
Participant
536 Points
73 Posts
     

You can use following for instagram to parse tags:

String.prototype.parseInstaHashtag = function () {
          return this.replace(/[#]+[A-Za-z0-9-_]+/g, function (t) {
              var tag = t.replace("#", "")
              return t.link("https://www.instagram.com/explore/tags/" + tag);
          });
      };
Posted On: 13-Sep-2017 04:49
banner

Blog

Active User (0)

No Active User!
 Log In to Chat