gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:17:15 25 4
gpt4 key购买 nike

我正在尝试使用以下代码从字符串中查找所有 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;
}

它工作正常,但当涉及到电子邮件 ID 时,它也显示 ma​​ilto。我不想显示。

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

任何人都可以帮助我避免显示 Mailto 和电子邮件 ID。

谢谢。

最佳答案

更新的代码/脚本
我想这就是您要找的东西

但最初我会从此正则表达式部分删除冒号 [-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>

关于javascript - 如何在查找字符串中的所有 url 时停止显示 mailto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58056553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com