📜  js 检测字符串中的链接 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:30.005000             🧑  作者: Mango

代码示例1
function replaceURLs(message) {
  if(!message) return;
 
  var urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
  return message.replace(urlRegex, function (url) {
    var hyperlink = url;
    if (!hyperlink.match('^https?:\/\/')) {
      hyperlink = 'http://' + hyperlink;
    }
    return '' + url + ''
  });
}
 
replaceURLs("Visit www.cluemediator.com and subscribe us on https://www.cluemediator.com/subscribe for regular updates.")
// Output: Visit www.cluemediator.com and subscribe us on https://www.cluemediator.com/subscribe for regular updates.