gpt4 book ai didi

javascript - jQuery 检测并验证 URL

转载 作者:搜寻专家 更新时间:2023-10-31 23:11:58 26 4
gpt4 key购买 nike

假设我有这段文字:

Hi, my name is Pablo and my home page is <a href="http://zad0xsis.net" rel="noreferrer noopener nofollow">http://zad0xsis.net</a>

如何检测和验证 URL,以便它自动将其包装到 <a href="the detected url">the detected url</a> 中?谢谢!

最佳答案

你可以使用正则表达式:

var s = 'Hi, my name is Pablo and my home page is http://zad0xsis.net';
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
s = s.replace(exp,"<a href=\"$1\">$1</a>");
alert(s);

Live demo .

关于javascript - jQuery 检测并验证 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7705501/

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