Обновленный код / скрипт
Я думаю, это то, что вы ищете
, но сначала я бы удалил двоеточие из этого раздела регулярных выражений [-a-zA-Z0-9@%._\+~#=]
(Кроме первого двоеточия после (http(s)?:\/\/)
)
document.getElementById("doIt").addEventListener("click", function(){
var urlRegex = /(http(?:s)?:\/\/)?(www\.)?([-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*))/g;
document.getElementById("result").innerHTML = document.getElementById("links").value.replace(urlRegex,
function (url) {
var extraText = /@/gi.test(url) ? "mailto:":"";
return '<a href="' +extraText+ url + '" title="'+ url +'">' + url + '</a>';
});
});
<textarea id="links" rows="4" cols="50">
Billions of people abc@gmail.com around the world are still https://www.wikipedia.org/ without internet access. Loon is a network of balloons traveling on the edge of space, delivering connectivity to people in unserved and underserved communities around the world.
</textarea>
<br />
<button id="doIt"> replace </button>
<br />
<div id="result">
</div>
Обновление изменить все ссылки, кроме "Адреса электронной почты:"
document.getElementById("doIt").addEventListener("click", function(){
var urlRegex = /(http(?:s)?:\/\/)?(www\.)?([-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*))/g;
document.getElementById("result").innerHTML = document.getElementById("links").value.replace(urlRegex,
function (url) {
//Check if url is an email (this regex could be improved)
return (/.+@.+/gi.test(url))? url :'<a href="' + url + '" title="'+ url +'">' + url + '</a>';
});
});
<textarea id="links" rows="4" cols="50">
Billions of people abc@gmail.com around the world are still https://www.wikipedia.org/ without internet access. Loon is a network of balloons traveling on the edge of space, delivering connectivity to people in unserved and underserved communities around the world.
</textarea>
<br />
<button id="doIt"> replace </button>
<br />
<div id="result">
</div>