2016-07-27 46 views
0

想不到一個好的標題對不起! 它做了什麼! 我已經ajson文件看起來有點像這樣:「你累了https://google.com/index.html \ n \ n爲什麼不嘗試https://bing.com/index.html獲取更多的價值

我想什麼它做的就是點擊鏈接來實現 文本對顯示的文本頁面只是成爲url的域名 例如google.com

我有它的工作到一個點。很好地得到第一個網址都格式正確,但不是第二個。

var url = "You tired of https://google.com/index.html \n\n why not try https://bing.com/index.html "; 
 
linkify(url); 
 
    function linkify(url) { 
 
     
 
     alert(url); 
 
     
 
      var domain; 
 
      //find & remove protocol (http, ftp, etc.) and get domain 
 
      if (url.indexOf("://") > -1) { 
 
       domain = url.split('/')[2]; 
 
      } 
 
      else { 
 
       domain = url.split('/')[0]; 
 
      } 
 

 
      //find & remove port number 
 
      domain = domain.split(':')[0]; 
 
     alert(domain); 
 
    
 
     var replacedText, replacePattern1, replacePattern2, replacePattern3; 
 

 
     //URLs starting with http://, https://, or ftp:// 
 
     replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim; 
 
     replacedText = url.replace(replacePattern1, '<a href="$1" target="_blank">'+domain+'</a>'); 
 

 
     //URLs starting with "www." (without // before it, or it'd re-link the ones done above). 
 
     replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim; 
 
     replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">'+domain+'</a>'); 
 

 
     //Change email addresses to mailto:: links. 
 
     replacePattern3 = /(([a-zA-Z0-9\-\_\.])[email protected][a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim; 
 
     replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>'); 
 

 
     
 
     document.getElementById('out').innerHTML = replacedText;}
<span id="out"></span>

回答

0

我看到你已經解決您的問題。但我想我會發布我的解決方案。誰知道,你可能會更喜歡它。希望能幫助到你。

https://jsfiddle.net/90m6ss8b/2/

var url = "You tired of https://google.com/index.html \n\n why not try https://bing.com/index.html "; 
linkify(url); 

function linkify(url) { 
    var matches = url.match(/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm); 

    alert(matches); 

    for (i = 0; i < matches.length; i++) { 
    if (matches[i].indexOf("://") > -1) { 
     alert(matches[i]); 
     matches[i] = matches[i].split('/')[2]; 
     alert(matches[i]); 
    } 
    } 

    alert(matches); 

    var replacedText, replacePattern1, replacePattern2, replacePattern3; 
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/im; 
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/im; 
    replacePattern3 = /(([a-zA-Z0-9\-\_\.])[email protected][a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/im; 

    replacedText = replaceIndex(url, replacePattern1,'<a href="$1" target="_blank">' + matches[0] + '</a>'); 
    replacedText = replaceIndex(replacedText, replacePattern2,'$1<a href="http://$2" target="_blank">' + matches[0] + '</a>'); 
    replacedText = replaceIndex(replacedText, replacePattern3, '<a href="mailto:$1">$1</a>'); 
    replacedText = replaceIndex(replacedText, replacePattern1,'<a href="$1" target="_blank">' + matches[1] + '</a>'); 
    replacedText = replaceIndex(replacedText, replacePattern2,'$1<a href="http://$2" target="_blank">' + matches[1] + '</a>'); 
    replacedText = replaceIndex(replacedText, replacePattern3, '<a href="mailto:$1">$1</a>'); 

    function replaceIndex(string, pattern, repl) { 
    return string.replace(pattern, function(match, i) { 
     if (i === match) return repl; 
     return match; 
    }); 
    } 

    document.getElementById('out').innerHTML = replacedText; 
} 
+0

它運作良好。謝謝,但是當鏈接的數量與發佈後的數量不同時,它可能會有點擊或錯過。我知道我可以改變[1]到[2]等等。你的方式是更有條理的,我的更多是一個保險工作。 –

0

var url = "You tired of https://google.com/index.html \n\n why not try https://bing.com/index.html "; 
 
    linkify(url); 
 
    function linkify(url) { 
 

 

 
     var replacedText, replacePattern1, replacePattern2, replacePattern3, i; 
 

 
     //URLs starting with http://, https://, or ftp:// 
 
     replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim; 
 
     replacedText = url.replace(replacePattern1, '<a href="$1" class="urlcouint" target="_blank" id="xurl">$1</a>'); 
 

 
     //URLs starting with "www." (without // before it, or it'd re-link the ones done above). 
 
     replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim; 
 
     replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$1</a>'); 
 

 
     //Change email addresses to mailto:: links. 
 
     replacePattern3 = /(([a-zA-Z0-9\-\_\.])[email protected][a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim; 
 
     replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>'); 
 

 

 
     document.getElementById('out').innerHTML = replacedText;} 
 
    
 
     var nubero = ($('a.urlcouint').length); 
 
     for(var i = 0; i < nubero; i++) { 
 
      var domain, url; 
 
      url = document.getElementById('xurl').href; 
 
     //find & remove protocol (http, ftp, etc.) and get domain 
 
     if (url.indexOf("://") > -1) { 
 
      domain = url.split('/')[2]; 
 
     } 
 
     else { 
 
      domain = url.split('/')[0]; 
 
     } 
 

 
     //find & remove port number 
 
     domain = domain.split(':')[0]; 
 
     alert(domain); 
 
     document.getElementById('xurl').innerHTML = domain; 
 
     document.getElementById('xurl').setAttribute('id', 'xurldone');}
<span id="out"></span> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>