2012-10-26 52 views
0

我已經有了兩個正則表達式的功能解析網址,並從JSON Twitter的響應回覆,但我試圖把它擴大到解析哈希標籤藏漢但我正在逐漸顯示在主題標籤的。安迪undefinedJavaScript的散列標籤解析功能

// process links, reply and hash tags 
tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) { 
     return '<a href="'+url+'">'+url+'</a>'; 
    }).replace(/[email protected]([_a-z0-9]+)/ig, function(reply) { 
     return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>'; 
    }).replace(/#([a-zA-Z0-9]+)/g), function(hash) { 
     return '<a class="hashtag" target="_blank" href="http://twitter.com/#search?q='+$1+'">#'+$1+'</a>'; 
    }; 

任何指向我要去哪裏的錯?

+0

'tweet'的內容是什麼? – SilentGhost

+0

tweet是'另一個垃圾推文#測試'和輸出是'另一個垃圾推文undefined' – martincarlin87

+0

爲什麼你不使用'哈希'變量? – SilentGhost

回答

1

在過去的功能替代需要一個哈希參數,使用,在你的回報,而不是$ 1

1

$ 1變量是不確定的,與hash.substring更換(1)

此外,您需要在匿名函數聲明之後關閉最後一次函數調用。

tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) { 
    return '<a href="'+url+'">'+url+'</a>'; 
}).replace(/[email protected]([_a-z0-9]+)/ig, function(reply) { 
    return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>'; 
}).replace(/#([a-zA-Z0-9]+)/g, function(hash) { 
    return '<a class="hashtag" target="_blank" href="http://twitter.com/#search?q='+hash.substring(1)+'">#'+hash.substring(1)+'</a>'; 
}); 
+0

明白了,謝謝,我在正則表達式後面加了左括號,並且它本應該在最後的功能 – martincarlin87