想不到一个好的标题对不起! 它做了什么! 我已经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>
它运作良好。谢谢,但是当链接的数量与发布后的数量不同时,它可能会有点击或错过。我知道我可以改变[1]到[2]等等。你的方式是更有条理的,我的更多是一个保险工作。 –