Identify URL links in strings Exclude image addresses

Requirement: Identify the URL link in the string, excluding the image address, and add a label to the URL link

function handleStr () {
    
    
	// backStr 需要处理得字符串
	var pattern = /(https?:\/\/[^\s]+)/g; // 识别所有http的链接 包括图片四肢
      let urlArr = backStr.match(pattern);  //拿到所有http链接的集合数组
      let reg = /(https?:[^:<>"]*\/)([^:<>"]*)(\.((png!thumbnail)|(png)|(jpg)|(webp)))/; //仅识别图片的地址
      for (let j = 0; j < urlArr.length; j++) {
    
    
        if (!reg.test(urlArr[j])) {
    
      //在这里把图片的地址区分出来
        // 给筛选出来的不是图片地址的链接加上a标签
          backStr = backStr.replace(
              urlArr[j],
              "<a href=" + urlArr[j] + " class='a-link'>" + urlArr[j] + "</a>"
          );
        }
      }
      return linefeed(backStr);
}

I didn’t find a regular expression that can directly distinguish the address from an image.
Anyone who knows it can comment and give advice.

END wishes you a day of peace of mind and a day where you don’t test and scold the product hahaha

Guess you like

Origin blog.csdn.net/weixin_44227395/article/details/130771960