检测到 link 以 www. 开头的问题,替换为一个元素
Detected link problem staring with www., replace with a element
当我匹配 http
或 https
时,我可以将其转换为 a
元素,但出于某种原因,无论我尝试什么,它都不会对 www.
做出反应有人解释一下吗?
$("[name='text']").each(function(element) {
let str = $(this).text();
if (str.match('http')||str.match('https')) {
var link= str.replace(/(https?:\/\/[^\s]+)/g,"<a href='' target='_blank' ></a>");
}
if (str.match('www')) {
var link= str.replace(/(www?:\/\/[^\s]+)/g,"<a href='' target='_blank' ></a>");
}
$(this).html(link);
console.log(link);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div name="text">https://whosebug.com/ https link</div>
<div name="text">http://whosebug.com/ http link</div>
<div name="text">www.whosebug.com/ www link</div>
根据@charlietfl 的评论,我为自己写了这篇文章,如果有人需要的话(我认为它涵盖了大多数情况):
$("[name='text']").each(function() {
let str = $(this).text();
//console.log(str);
if (str.match('www') || str.match('http') || str.match('https')) {
var link = "";
if (str.match('http') || str.match('https')) {
link = str.replace(/(https?:\/\/[^\s]+)/g, "<a href='' target='_blank' ></a>");
}
if (str.match("www") && !str.includes("http")) {
var str2 = str.replace("www", "http://www");
link = str2.replace(/(https?:\/\/[^\s]+)/g, "<a href='' target='_blank' ></a>");
}
$(this).html(link);
console.log(link);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div name="text">test1 https://whosebug.com/ https link</div>
<div name="text">test2 http://whosebug.com/ http link</div>
<div name="text">test3 http://www.whosebug.com/ http link</div>
<div name="text">test4 https://www.whosebug.com/ http link</div>
<div name="text">test5 www.whosebug.com/ link</div>
<div name="text">test6 https://www.whosebug.com/www-test with another www inside</div>
<div name="text">test7 nolink</div>
当我匹配 http
或 https
时,我可以将其转换为 a
元素,但出于某种原因,无论我尝试什么,它都不会对 www.
做出反应有人解释一下吗?
$("[name='text']").each(function(element) {
let str = $(this).text();
if (str.match('http')||str.match('https')) {
var link= str.replace(/(https?:\/\/[^\s]+)/g,"<a href='' target='_blank' ></a>");
}
if (str.match('www')) {
var link= str.replace(/(www?:\/\/[^\s]+)/g,"<a href='' target='_blank' ></a>");
}
$(this).html(link);
console.log(link);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div name="text">https://whosebug.com/ https link</div>
<div name="text">http://whosebug.com/ http link</div>
<div name="text">www.whosebug.com/ www link</div>
根据@charlietfl 的评论,我为自己写了这篇文章,如果有人需要的话(我认为它涵盖了大多数情况):
$("[name='text']").each(function() {
let str = $(this).text();
//console.log(str);
if (str.match('www') || str.match('http') || str.match('https')) {
var link = "";
if (str.match('http') || str.match('https')) {
link = str.replace(/(https?:\/\/[^\s]+)/g, "<a href='' target='_blank' ></a>");
}
if (str.match("www") && !str.includes("http")) {
var str2 = str.replace("www", "http://www");
link = str2.replace(/(https?:\/\/[^\s]+)/g, "<a href='' target='_blank' ></a>");
}
$(this).html(link);
console.log(link);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div name="text">test1 https://whosebug.com/ https link</div>
<div name="text">test2 http://whosebug.com/ http link</div>
<div name="text">test3 http://www.whosebug.com/ http link</div>
<div name="text">test4 https://www.whosebug.com/ http link</div>
<div name="text">test5 www.whosebug.com/ link</div>
<div name="text">test6 https://www.whosebug.com/www-test with another www inside</div>
<div name="text">test7 nolink</div>