美文网首页
jquery获取文章中的url链接,并在链接上设置a标签

jquery获取文章中的url链接,并在链接上设置a标签

作者: 默色留恋 | 来源:发表于2023-03-15 16:01 被阅读0次
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)); 
    }
})

相关文章

网友评论

      本文标题:jquery获取文章中的url链接,并在链接上设置a标签

      本文链接:https://www.haomeiwen.com/subject/iuilrdtx.html