想不出一个好头sorry对不起! 它有什么作用! 我有一个ajson文件看起来有点像这样“你厌倦了https://google.com/index.html \ n \ n为什么不试试https://bing.com/index.html”
我希望它能让文本成为点击链接 让页面上显示的文本只是网址的域名 例如google.com
我让它发挥作用。它可以很好地获得第一个url所有格式正确但不是第二个。
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\-\_\.])+@[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 :(得分: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\-\_\.])+@[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>
答案 1 :(得分: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\-\_\.])+@[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;
}