微信小程序rich-text 文本首行缩进和图片居中和富文本rich-text 解析多个空格不成功 &nbsp

微信小程序开发使用rich-text组件渲染html格式的代码,常常因为不能自定义css导致文本不能缩进,以及图片不能居中等问题,这里可以考虑使用js的replace方法,替换字符串,然后在渲染的同时加载行内样式。

复制代码
//获取字符串的图片路径并替换
let content = res.data.articleVo.content
let re = /<img [^>]*src=['"]([^'"]+)[^>]*>/gi;
let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i // 匹配图片中的src
let reHttp = new RegExp("http");
let imgArr = content.match(re);
for (let i = 0; i < imgArr.length; i++) {
  let imgSrc = imgArr[i].match(srcReg);
  if (!reHttp.test(imgSrc[1])) {
    content = content.replaceAll(imgSrc[1], URL.imghhx + imgSrc[1]);
  }
}
//先去掉本身的style
const regex = new RegExp('style="max-width:100%;" ', 'gi');
let contentIMg  = content.replace(regex, '');
//文本首行缩进和图片居中
let article = contentIMg.replace(/(\<img|\<p)/gi, function ($0, $1) {
  return {
    "<img": '<img style="width:100%;height:auto;display:block;" ',
    "<p": '<p style="text-indent: 24px;" ',
    "<article": "<div",
    "</article": "</div",
    "<header": "<div",
    "</header": "</div"
  } [$1];
});  

小程序富文本rich-text 解析多个空格不成功 &nbsp 解决方案

&nbsp; 替换为 &ensp;即可

复制代码
let str = '<p>&ensp; &ensp; 今天是阳光明媚的一天</p>'.replace(/&nbsp;/g, '&ensp;')
相关推荐
00后程序员张13 小时前
iPhone 无需越狱文件管理 使用Keymob查看导出文件
android·ios·小程序·https·uni-app·iphone·webview
毕设源码-朱学姐13 小时前
【开题答辩全过程】以 基于微信小程序的运动减肥管理系统设计与实现为例,包含答辩的问题和答案
微信小程序·小程序
2501_9160088913 小时前
Unity3D iOS 应用防篡改实战 资源校验、 IPA 二进制保护
android·ios·小程序·https·uni-app·iphone·webview
2501_9159090613 小时前
MachObfuscator全面解析:Apple平台Mach-O应用程序混淆技术指南
macos·ios·小程序·uni-app·objective-c·cocoa·iphone
大鹏说大话16 小时前
微信小程序制作一个需要多少钱?
微信小程序
2501_915918411 天前
苹果App Store上架审核卡住原因分析与解决方案指南
android·ios·小程序·https·uni-app·iphone·webview
用户700980735731 天前
从零开发一个微信记账小程序,零依赖、附完整源码
微信小程序
焦糖玛奇朵婷1 天前
盲盒小程序开发|解锁开箱新体验[特殊字符]
大数据·开发语言·程序人生·小程序·软件需求
左师佑图1 天前
微信小程序组件事件冒泡问题排查与解决方案
微信小程序·小程序
树下水月2 天前
微信小程序接口,必须使用https的443端口吗?
微信小程序·小程序·https