jQuery regex从文本链接-添加不需要的域到链接

jQuery regex link from text - adds unwanted domain to link

本文关键字:链接 不需要 添加 regex 文本 jQuery      更新时间:2023-09-26

所以,除了链接部分,我的脚本还可以。这是一个jquery推特提要脚本,可以拉入我的推文。它通过regex生成的链接应该是-http://twitter.com-相反,它显示为http://www.mydomain.com/http://twitter.com-obv不起作用。

澄清一下——当一个推特提要中有一个文本链接时,正则表达式会将其变成一个适当的链接,但会将页面的当前url放在它前面。

<script language="JavaScript">
$.getJSON("https://twitter.com/statuses/user_timeline.json screen_name=twitternamehere&count=2&callback=?",
function(data){
$.each(data, function(i,item){
ct = item.text;
mytime = item.created_at;
var strtime = mytime.replace(/('+'S+) (.*)/, '$2 $1')
var mydate = new Date(Date.parse(strtime)).toLocaleDateString();
var mytime = new Date(Date.parse(strtime)).toLocaleTimeString();        
ct = ct.replace(/http:'/'/'S+/g,  '<a href="$&" target="_blank">$&</a>');
ct = ct.replace(/'s(@)('w+)/g,    ' @<a href="http://twitter.com/$2" target="_blank">$2</a>');
ct = ct.replace(/'s(#)('w+)/g,    ' #<a href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
 $("#tweetRow").append("<div class='container'><div class='date'> "+mydate+" "+mytime+" </div><div class='tweet'> "+ct+" </div></div>");
            });
$('.container:last').addClass('container2'); 
            });
</script>
function processTwitLnk(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;
}
ct=processTwitLnk(ct);

希望这能有所帮助!!