jquery获取文章中的url链接,并在链接上设置a标签
<div class="divparent">链接:https://pan.baidu.com/s/1coIaX72-uPasKhEUEPqFNA 提取码:2321</div>
<script>
$(function(){
$('.divparent').each(function(){
let regexp = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|\&|-)+)/g; //正则匹配
//内容设置a标签
txt1=$(this).text().replace(regexp,function($url){
return "<a href='" + $url + "' target='_blank'>" + $url + "</a>";
});
$(this).html(txt1);
})
});
</script>
1212.png
获取url
function httpString(s) {
var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
s = s.match(reg);
console.log(s)
return(s)
}
$('.divparent').each(function(){
httpString($(this).text());
})
截取特定的文章中url
var str = $('.divparent').text();
$('.divparent').each(function(){
if($(this).text().slice(0,3)=='链接:'){
console.log($(this).text().slice(-8))
$(this).html('链接:<a href="'+$(this).text().slice(3,-8)+'">'+$(this).text().slice(3,-8)+'</a>'+$(this).text().slice(-8));
}
})