首页 > Web开发 > 详细

好用的JS 函数库

时间:2021-05-13 19:54:54      阅读:22      评论:0      收藏:0      [点我收藏+]

前言

主要是用于记录比较好用的函数库,方便以后使用

URL字符串替换为 A标签

function replaceURLWithHTMLLinks(text) {
    var exp = /(http:\/\/|ftp:\/\/|https:\/\/|www.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/g;

    return text.replace(exp,function(url){
        return "<a href=‘"+url+"‘>"+url+"</a>"
    }); 
}

console.log(replaceURLWithHTMLLinks("www.google.com"))
console.log(replaceURLWithHTMLLinks("www.baidu.com/#/a?c=21"))
console.log(replaceURLWithHTMLLinks("http://www.baidu.com"))
console.log(replaceURLWithHTMLLinks("http://www.baidu.com/#/a?c=12"))
console.log(replaceURLWithHTMLLinks("我的http://www.baidu.com"))
console.log(replaceURLWithHTMLLinks("http://www.baidu.com我的"))
console.log(replaceURLWithHTMLLinks("我的http://www.baidu.com/我的"))
console.log(replaceURLWithHTMLLinks("12.png"))
console.log(replaceURLWithHTMLLinks("www.baodi.com/ 12"))

好用的JS 函数库

原文:https://www.cnblogs.com/clintisgoodboy/p/14764202.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!