请帮我正则表达.
我发现这很好的代码和平:
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)));
}
};
}();
Run Code Online (Sandbox Code Playgroud)
但它不能正常工作.
首先在我的页面中可以存在<b>@username</b>并且为此导致正则表达式不起作用(我想我需要将这些字符"<"和">"附加到"at function".但是如何?)
在我的页面中的"哈希"函数中的第二个,在查询中可以存在其他非a-zA-Z字符).例如"такиесимволы"或"ñ"或其他.并格式化字符串像#<a href="twitter.com/?q=Catalu">Catalu</a>ña的#Cataluña词
请帮我.谢谢!
小智 15
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3875 次 |
| 最近记录: |