小程序富文本字符串处理方法

 封装方法:

parseRich:function(content) {
  const singalList = [
    ['"', '"'],
    ['&', '&'],
    ['<', '<'],
    ['>', '>'],
    [' ', ' ']
  ];
  const tagList = ['p', 'span', 'img', 'a', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'font', 'b', 'i', 'u', 'code', 'table', 'tr', 'td', 'th']
  singalList.forEach(i => {
    content = content.replace(new RegExp(i[0], 'g'), i[1])
  })
  tagList.forEach(i => {
    content = content.replace(new RegExp(`<${i} `, 'gi'), `<${i} class="rich_${i}" `)
  })
  return content;
}

你可能感兴趣的:(小程序,java,开发语言)