微信小程序中提取富文本内容中的文本

微信小程序项目中遇到父页需要提前显示一行子页的富文本内容,需对富文本格式做处理,提取文本内容:

export function toRichText(html){
  // 对应替换的的内容
  const rep = {
    "‘": "‘",
    "’": "’",
    "“": "“",
    "”": "”",
    "…": "…",
    "·": "·",
    "&": "&",
    "&lt;": "<",
    "&gt;": ">",
   "&nbsp;":" " } return html.replace(/<[^>]+>/g, "") .replaceAll(/\r|\n|\\s| | | /ig, "") .replaceAll(new RegExp(Object.keys(rep).join('|'), 'ig'), function ($0) { return rep[$0]; }) }

 参考:https://blog.csdn.net/weixin_55556204/article/details/124488693

 

posted @ 2022-10-26 18:16  月下云生  阅读(477)  评论(0)    收藏  举报