正则表达式如何替换Twitter链接 [英] regex how to replace twitter links

查看:41
本文介绍了正则表达式如何替换Twitter链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我提供正则表达式.

我发现代码很好:

  var ify = function(){返回 {链接":function(t){返回t.replace(/(^ | \ s +)(https * \:\/\/\ S + [^ \.\ s +])/g,function(m,m1,link){返回m1 +'< a href ='+链接+'>'+((link.length> 25)?link.substr(0,24)+'...':link)+'</a>';});},"at":function(t){返回t.replace(/(^ | \ s +)\ @([a-zA-Z0-9 _] {1,15})/g,function(m,m1,m2){返回m1 +'@< a href ="http://twitter.com/'+ m2 +'">'+ m2 +'</a>';});},哈希":function(t){返回t.replace(/(^ | \ s +)\#([a-zA-Z0-9 _] +)/g,function(m,m1,m2){返回m1 +'#< a href ="http://search.twitter.com/search?q=%23'+ m2 +'">'+ m2 +'</a>';});},干净":function(tweet){返回this.hash(this.at(this.link(tweet)));}};}(); 

但是它不能正常工作.

首先,在我的页面中可能会有< b> @ username</b> ,因此正则表达式无法正常工作(我想我需要在此字符后加上<"和">"到"at函数".但是如何?)

在我页面的哈希"函数中,第二个查询中可能还有其他非a-zA-Z字符).例如такиесимволы"或ñ"或其他.格式化后的字符串看起来像#Cataluña字词的#< a href ="twitter.com/?q=Catalu"> Catalu</a>ña>

请帮助我.谢谢!

解决方案

 函数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>");返回文字;} 

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

这篇关于正则表达式如何替换Twitter链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆