2011-10-15 73 views
1

可以說我有這樣的文字 「這是一個長文本,它包含150個字符,你可以在這個鏈接http://www.somewebsite.com/RDFCCSDVDS上找到更多關於此文本的信息。如何使用jquery在文本中查找鏈接?

所以在上面的文字我想找到該鏈接並將其轉換爲鏈接,以便當用戶點擊它時,用戶將被直接帶到這個網站。

我該如何實現這個目標?

+0

可能重複[如何用鏈接替換原來的URL?](http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links) – Eric

回答

3

我懷疑,我可以多已在此改善,但在一分鐘,這是我能提供的最好的(雖然我認爲某種替代的可能更有效地工作):

var $words = $('p').text().split(' '); 
for (i in $words) { 
    if ($words[i].indexOf('http://') == 0) { 
     $words[i] = '<a href="' + $words[i] + '">' + $words[i] + '</a>'; 
    } 
} 

$('p').html($words.join(' ')); 

JS Fiddle demo

稍微改善上述的版本(但仁慈的上帝,這是醜陋的...):

var punctuation = ['!',"'",'"',',','.']; 
$('p').each(
    function(){ 
     $words = $(this).text().split(' '); 
     for (i in $words){ 
      if ($.inArray($words[i].charAt(0),punctuation) > -1 && $words[i].indexOf('http://') == 1){ 
       alert($words[i]); 
      } 
      else if ($.inArray($words[i].charAt($words[i].length - 1),punctuation) > -1 && ($words[i].indexOf('http://') == 1 || $words[i].indexOf('http://') == 0)){ 
       $words[i] = '<a href="'+$words[i].substring(0,$words[i].length-1)+'">' + $words[i].substring(0,$words[i].length-1) + '</a>' + $words[i].charAt($words[i].length-1); 
      } 
      else if ($words[i].indexOf('http://') == 0){ 
       $words[i] = '<a href="' + $words[i] + '">' + $words[i] + '</a>'; 
      } 
     } 
     $(this).html($words.join(' ')); 
    }); 

JS Fiddle demo。儘管(例如"http://google.com"等文本),我不太確定如何處理引用的鏈接;但是,我並不清楚如何處理引用的鏈接。而且,說實話,我認爲那個正則表達式可能就是這個問題的更好辦法。

+0

有時,正則表達式是正確的錘子 – Eric

+0

在這種情況下?絕對!我對那裏的混亂有些驚訝(這很有效,但是......我知道**有很多情況,它沒有'if')。 –

-3

在jQuery中,你可以使用標籤在你的選擇來獲取頁面即所有鏈接.. $("a")

所以,如果你想要做的事到頁面上的每一個環節,你可以使用

$("a").each(function() { 
    //your code here. you can access the link by using $(this) 
}); 
+0

我想要的是將此鏈接從計劃文本轉換爲HTML錨鏈接,以便它可以點擊。目前用戶必須選擇並複製並粘貼以訪問該網站。 – doforumda

0

使用正則表達式

var e = /http:\/\/([A-Za-z0-9\.-_]{2,}\.)+[A-Za-z]{2,}(\/.+)/, 
    s = "This is a long text. It contains 150 characters. You can find more about this text on this link http://www.somewebsite.com/RDFCCSDVDS"; 

if (s.match(e)) { 
    var url = RegExp['$&']; 
    s = s.replace(url, '<a href="' + url + '">' + url + '</a>'); 
} 
+0

如果鏈接後有標點符號,或者有多個鏈接要替換,那麼正則表達式不起作用。你用正則表達式。你現在有兩個問題。 – Eric

6

使用正則表達式:

$('p').html(function(i, text) { 
    return text.replace(
     /\bhttp:\/\/([\w\.-]+\.)+[a-z]{2,}\/.+\b/gi, 
     '<a href="$&">$&</a>' 
    ); 
}); 

demo

+1

你會如何迎合你的正則表達式中的https? – Morgs

0

@Eric解決方案是輝煌的,但我發現,可能是錯的,如果在URL後有空格。嘗試他的jsfiddle例如有這樣的文字:

<p>This is a long text. It contains 150 characters. You can find more about this text on this link http://api.som-ewebsite.com/RDFCCS-VDS/#!/?p1=foo&p2=bar right here</p>​​​​​​​​​​​​​​​​​​​​​​​ 

所以我改變了正則表達式的最新部分攔截幾乎每一個URL友好字符排除的空間,這是一個網址「終結者」,我(在當然,這並不是真實的世界,但是我找不到一種方法來將普通空格和屬於URL的空格用純文本分開)。

$('p').html(function(i, text) { 
    return text.replace(
     /\bhttp:\/\/([\w\.-]+\.)+[a-z]{2,}([\/\w\.\-\_\?\=\!\#,\&amp;]+)/gi, 
     '<a href="$&">$&</a>' 
    ); 
});​​ 

我認爲它可以改善,建議,歡迎一如既往:)

編輯:

無論如何,我認爲這是周圍最好的解決辦法:的How to replace plain URLs with links?

相關問題