前端解析文本中URL并处理为超链接

将文本中的url正则判断出来,并改为超链接形式

前端解析文本中URL并处理为超链接_第1张图片

  const renderFormatUrl = (content: string) =>{
    if (!content) {
      return null;
    }
    let urlPattern = /(https?:\/\/|www\.)[a-zA-Z_0-9\-@]+(\.\w[a-zA-Z_0-9\-:]+)+(\/[\(\)~#&\-=?\+\%/\.\w]+)?/g; // eslint-disable-line no-useless-escape
    let preContent = content.replace(urlPattern, function(match: string) {
      let href = match;
      if (match.indexOf("http") === -1) {
        href = "http://" + match;
      }
      return '' + match + "";
    });
  
    return (
      
") }} /> ); }

你可能感兴趣的:(js,javascript,前端)