2011-05-02 39 views
2

我使用這個偉大的插件結束鏈接: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 
+0

怎麼樣在**括號鏈接**? (http://example.com) – Kobi 2011-05-02 17:01:05

+0

Kobi,我真的沒有想到...我認爲它不能這樣做......嗯...... – Santiago 2011-05-02 17:13:26

+0

Kobi的評論大概是它工作的原因它確實如此。然而,在我看來,應該有可能實現如下邏輯:如果URL在其後有一個'(',並且沒有')',那麼假設最後的')'是的網址。否則,假設它不是。或者,如果你想變得很花哨,你可以在沒有paren的情況下對URL進行AJAX'HEAD'調用。如果你得到一個404,把它與paren鏈接起來。可能有些理由不這麼做,但這可能是一個有趣的實驗。 – MatrixFrog 2011-05-06 05:22:12

回答

1

從我發現,the regex expression found here(最初由John Gruber of Daring Fireball創建,由naren1012修改),似乎這樣的伎倆

實施,將這段代碼:

var noProtocolUrl = /(^|["'(\s]|&lt;)(www\..+?\..+?)((?:[:?]|\.+)?(?:\s|$)|&gt;|[)"',])/g, 
     httpOrMailtoUrl = /(^|["'(\s]|&lt;)((?:(?:https?|ftp):\/\/|mailto:).+?)((?:[:?]|\.+)?(?:\s|$)|&gt;|[)"',])/g, 
     linkifier = function (html) { 
      return html 
         .replace(noProtocolUrl, '$1<a href="<``>://$2">$2</a>$3') // NOTE: we escape `"http` as `"<``>` to make sure `httpOrMailtoUrl` below doesn't find it as a false-positive 
         .replace(httpOrMailtoUrl, '$1<a href="$2">$2</a>$3') 
         .replace(/"<``>/g, '"http'); // reinsert `"http` 
     }, 

有了這個代碼:

var noProtocolUrl = /(^|["'(\s]|&lt;)(www\..+?\..+?)((?:[:?]|\.+)?(?:\s|$)|&gt;|[)"',])/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 html 
         .replace(noProtocolUrl, '$1<a href="<``>://$2">$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">$1</a>') 
         .replace(/"<``>/g, '"http'); // reinsert `"http` 
     }, 
+1

布拉德,這是行不通的,但我認爲這可能是因爲在正則表達式的最後一個字符顯示錯誤:«â»»»»»»» – Santiago 2011-05-10 04:10:11

+0

謝謝布拉德,我只是看到原來的職位在DaringFireball和它的工作!我現在對「www.simple.com」鏈接有點問題,我編輯了這個問題...... – Santiago 2011-05-10 16:19:00