http://www.soulteary.com/2014/12/05/better-url-regexp-in-js.html

参考以上地址写的替换文本中URL的方法

 

function replaceSrc(txt){
var reg = /(((https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/ig;
var result = txt.replace(reg,function(item){
return "<a href='"+item+"' target='_blank'>"+item+"</a>";
});
return result;
}

 

EG:

var str = '1231www.baidu.com hahaha http://hao123.com 123123';

replaceSrc(str);

 

 

结果返回:1231 <a href="www.baidu.com" target="_blank">www.baidu.com</a> hahaha <a href="http://hao123.com" target="_blank">http://hao123.com</a> 123123

相关文章:

  • 2021-12-01
  • 2022-12-23
  • 2021-11-08
  • 2022-12-23
  • 2021-10-03
  • 2022-12-23
  • 2021-07-15
  • 2021-07-11
猜你喜欢
  • 2022-12-23
  • 2021-06-20
  • 2021-07-31
  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
相关资源
相似解决方案