我使用這個偉大的插件結束鏈接:https://github.com/maranomynet/linkify/blob/master/1.0/jquery.linkify-1.0.js的jQuery插件Linkify:無法處理在括號
鏈接文本操縱DOM。問題是像這樣的鏈接:http://en.wikipedia.org/wiki/The_Godfather_(novel)
的鏈接將是「http://en.wikipedia.org/wiki/The_Godfather_(novel」
可能我在linkify代碼來處理括號改變什麼等
感謝
PS:?!嘿嘿,看來能#1也使用此哈哈;)
編輯:
我剛剛在DaringFireball上看到了這篇文章,它的效果很好......問題在於像www.google.com這樣的簡單網址(我認爲它與第一個「noProtocolUrl」正則表達式有關。這是我有現在:
var noProtocolUrl = /(^|["'(\s]|<)(www\..+?\..+?)((?:[:?]|\.+)?(?:\s|$)|>|[)"',])/g,
httpOrMailtoUrl = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:(?:[^\s()<>.]+[.]?)+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»「」‘’]))/gi,
linkifier = function (html) {
return FormatLink(html
.replace(noProtocolUrl, '$1<a href="<``>://$2" rel="nofollow external" class="external_link">$2</a>$3') // NOTE: we escape `"http` as `"<``>` to make sure `httpOrMailtoUrl` below doesn't find it as a false-positive
.replace(httpOrMailtoUrl, '<a href="$1" rel="nofollow external" class="external_link">$1</a>')
.replace(/"<``>/g, '"http')); // reinsert `"http`
},
隨着「www.facebook.com」我得到這個(與rel和類屬性,就像旁邊的文字鏈接:
www.facebook.com" rel="nofollow external" class="external_link">www.facebook.com
怎麼樣在**括號鏈接**? (http://example.com) – Kobi 2011-05-02 17:01:05
Kobi,我真的沒有想到...我認爲它不能這樣做......嗯...... – Santiago 2011-05-02 17:13:26
Kobi的評論大概是它工作的原因它確實如此。然而,在我看來,應該有可能實現如下邏輯:如果URL在其後有一個'(',並且沒有')',那麼假設最後的')'是的網址。否則,假設它不是。或者,如果你想變得很花哨,你可以在沒有paren的情況下對URL進行AJAX'HEAD'調用。如果你得到一個404,把它與paren鏈接起來。可能有些理由不這麼做,但這可能是一個有趣的實驗。 – MatrixFrog 2011-05-06 05:22:12