所以,我的脚本可以分开链接部分。它是一个jquery twitter feed脚本,可以提取我的推文。 它通过正则表达式生成的链接应该是 - http://twitter.com - 相反它出现为http://www.mydomain.com/http://twitter.com - 哪个obv不起作用。
澄清一下 - 当有一个带有文本链接的推文来源时 - 正则表达式将其转换为正确的链接,但将页面当前的网址放在它前面。
<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>
答案 0 :(得分:0)
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);
希望这会有所帮助!!