admin管理员组文章数量:1420929
See line 8 in the code below, with the ment:
<script>
$(function(){
$.getJSON('.json?count=1&callback=?',twitterJSON);
function twitterJSON(data){
var twitterOut = '<p>'+data[0].text+'</p><strong>- '+data[0].user.name+'</strong>';
var twitterOutAt = twitterOut.replace(/\B@([\w-]+)/gm,'<a href="/$1">@$1</a>');
var twitterOutHash = twitterOutAt.replace(/\B#([\w-]+)/gm,'<a href="=$1">#$1</a>');
var twitterOutDone = twitterOutHash.replace(/(href="|<a.*?>)?[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g,'<a href="$1">$1</a>'); // not working :(
$('.twitter').html(twitterOutDone);
}
});
</script>
Any help refactoring the code would be very much appreciated!
For example: there must be a way to chain .replace
so one doesn't have to assign a new var
again and again. I've tried var twitterOut.replace().replace()...
but that doesn't seem to work :(
See line 8 in the code below, with the ment:
<script>
$(function(){
$.getJSON('http://twitter./status/user_timeline/TWITTER.json?count=1&callback=?',twitterJSON);
function twitterJSON(data){
var twitterOut = '<p>'+data[0].text+'</p><strong>- '+data[0].user.name+'</strong>';
var twitterOutAt = twitterOut.replace(/\B@([\w-]+)/gm,'<a href="http://twitter./$1">@$1</a>');
var twitterOutHash = twitterOutAt.replace(/\B#([\w-]+)/gm,'<a href="http://search.twitter./search?q=$1">#$1</a>');
var twitterOutDone = twitterOutHash.replace(/(href="|<a.*?>)?[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g,'<a href="$1">$1</a>'); // not working :(
$('.twitter').html(twitterOutDone);
}
});
</script>
Any help refactoring the code would be very much appreciated!
For example: there must be a way to chain .replace
so one doesn't have to assign a new var
again and again. I've tried var twitterOut.replace().replace()...
but that doesn't seem to work :(
-
2
Leaving the rest aside, you can certainly chain
replace
s (as areplace
returns the new string on which you can apply another replace), could you post the code that's not working for you? – deviousdodo Commented Dec 2, 2011 at 8:15
2 Answers
Reset to default 6Here is the working function I use:
jQuery.fn.urlize = function() {
if (this.length > 0) {
this.each(function(i, obj){
// making links active
var x = $(obj).html();
var list = x.match( /\b(http:\/\/|www\.|http:\/\/www\.)[^ <]{2,200}\b/g );
if (list) {
for ( i = 0; i < list.length; i++ ) {
var prot = list[i].indexOf('http://') === 0 || list[i].indexOf('https://') === 0 ? '' : 'http://';
x = x.replace( list[i], "<a target='_blank' href='" + prot + list[i] + "'>"+ list[i] + "</a>" );
}
}
$(obj).html(x);
});
}
};
In your case:
$('.twitter').urlize();
I found a solution here: https://stackoverflow./a/3890175/1076933
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Twitter</title>
</head>
<body id="top">
<script src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function() {
$.getJSON("http://twitter./status/user_timeline/envato.json?count=1&callback=?", twitterJSON);
function twitterJSON(data) {
var twitterOut = data[0].text;
twitterOut = twitterOut
.replace(/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim, '<a href="$1">$1</a>')
.replace(/\B#([\w-]+)/gm, '<a href="http://search.twitter./search?q=$1">#$1</a>')
.replace(/\B@([\w-]+)/gm, '<a href="http://twitter./$1">@$1</a>');
$("body").html(twitterOut);
};
});
</script>
</body>
</html>
Thanks to Silver Light as well, his function works a charm!
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Twitter</title>
</head>
<body id="top">
<script src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function() {
jQuery.fn.urlize = function() {
if (this.length > 0) {
this.each(function(i, obj) {
var x = $(obj).html();
var list = x.match(/\b(http:\/\/|www\.|http:\/\/www\.)[^ <]{2,200}\b/g);
if (list) {
for (i = 0; i < list.length; i++) {
var prot = list[i].indexOf("http://") === 0 || list[i].indexOf("https://") === 0 ? "" : "http://";
x = x.replace(list[i], "<a target='_blank' href='" + prot + list[i] + "'>" + list[i] + "</a>");
}
}
$(obj).html(x);
})
}
};
$.getJSON("http://twitter./status/user_timeline/envato.json?count=1&callback=?", twitterJSON);
function twitterJSON(data) {
var twitterOut = data[0].text;
twitterOut = twitterOut
.replace(/\B#([\w-]+)/gm, '<a href="http://search.twitter./search?q=$1">#$1</a>')
.replace(/\B@([\w-]+)/gm, '<a href="http://twitter./$1">@$1</a>');
$("body").html(twitterOut).urlize();
};
});
</script>
</body>
</html>
本文标签: javascriptHow to replace links in a string with clickable versions using jquery regexStack Overflow
版权声明:本文标题:javascript - How to replace links in a string with clickable versions using jquery regex? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745314863a2653105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论