繁体   English   中英

如何在字符串中查找所有 url 时停止显示 mailto

[英]How to stop showing mailto while finding all url's in a string

我正在尝试从使用以下代码的字符串中查找所有 URL。

Urlify :function (text) {
        var urlRegex = /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
    }

它工作正常,但是当涉及到 email id 时,它也显示mailto 。我不想显示。

< abc@test.com<mailto:abc@test.com>>

谁能帮助我避免将 Mailto 与email id 一起显示。

谢谢。

更新的代码/脚本
我想这就是你要找的

但最初我会从这个正则表达式部分删除冒号[-a-zA-Z0-9@%._\+~#=]

(除了(http(s)?:\/\/)之后的第一个冒号)

 document.getElementById("doIt").addEventListener("click", function(){ var urlRegex = /(http(?:s)?:\/\/)?(www\.)?([-a-zA-Z0-9@%._\+~#=]{2,256}\.[az]{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}\.[az]{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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM