小程序中富文本的处理(内含图片)

页面显示代码:

<rich-text v-if="haveData !==''" class="course-des" :nodes="formatRichText(lawsDetail.courseText)"></rich-text>

//处理富文本标签(内含图片)
        formatRichText(richText){
          if(richText != null){
                let newRichText= richText.replace(/<img[^>]*>/gi,function(match,capture){
                    match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
                    match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
                    match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
                    return match;
                });
                newRichText = newRichText.replace(/style="[^"]+"/gi,function(match,capture){
                    match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
                    return match;
                });
                newRichText = newRichText.replace(/<br[^>]*\/>/gi, '');
                newRichText = newRichText.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin:10px 0;"');
                return newRichText;
            }else{
                return null;
            }
        }

转载:https://www.cnblogs.com/lovelh/p/12747497.html

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2021-11-12
  • 2022-12-23
猜你喜欢
  • 2021-10-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
  • 2021-04-24
相关资源
相似解决方案