文本域带格式的内容,展示在div中,保存格式,并且url变成超链接

组件页

import React from "react";

const FormatDiv = (props = {}) => {
  if (!props.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 content = props.content.replace(urlPattern, function(match) {
    var href = match;
    if (match.indexOf("http") === -1) {
      href = "http://" + match;
    }
    return '' + match + "";
  });

  return (
    
") }} /> ); }; export default FormatDiv;

调用页

                

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