js找出字符串中的网址,并给网址加上超链接
<div class="text_area">
这是一个网址:https://www.baidu.com
</div>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
var content = $('.text_area').html();
var text_area_img =$('.text_area img').attr('src');
if (content.length > 0) {
var reg =/(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|\;|&|-)+)\b/g;
if(!reg.exec(text_area_img)){
content= content.replace(reg, "<a target='_blank' href='$1$2'>$1$2</a>");
}
$('.text_area').html(content);
}
</script>
这是一个网址:https://www.baidu.com
</div>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
var content = $('.text_area').html();
var text_area_img =$('.text_area img').attr('src');
if (content.length > 0) {
var reg =/(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|\;|&|-)+)\b/g;
if(!reg.exec(text_area_img)){
content= content.replace(reg, "<a target='_blank' href='$1$2'>$1$2</a>");
}
$('.text_area').html(content);
}
</script>