2016-07-27 47 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>