See line 8 in the code below, with the comment:
<script>
$(function(){
$.getJSON('http://twitter.com/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.com/$1">@$1</a>');
var twitterOutHash = twitterOutAt.replace(/\B#([\w-]+)/gm,'<a href="http://search.twitter.com/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 :(
replaces (as areplacereturns the new string on which you can apply another replace), could you post the code that's not working for you?