在获取富文本后,又只要显示部分内容,需要去除富文本标签,然后再截取其中一部分内容;然后就是过滤器,在微信小程序中使用还是挺多次的,在vue及react中也遇到过

1.富文本去除html标签

  • 去除html标签及 空格

let richText = ' <p style="font-size: 25px;color: white">       sdaflsjf的丰富及饿哦塞尔</p><span>dsfjlie</span>'; /* 去除富文本中的html标签 */
/* *、+限定符都是贪婪的,因为它们会尽可能多的匹配文字,只有在它们的后面加上一个?就可以实现非贪婪或最小匹配。*/
let content = richText.replace(/<.+?>/g, '');
console.log(content); /* 去除  */
content = content.replace(/ /ig, '');
console.log(content); /* 去除空格 */
content = content.replace(/\s/ig, '');
console.log(content);
  • 截取字符串

content = formatRichText(content);
console.log(content); /* 使用substring来截取字符串 */
if (content.length > 10) {
content = content.substring(0, 10) + '...';
}
console.log(content); /* 限制字数后添加省略号 */
function formatRichText(richText) {
let temporaryText = '';
/* 设置多长后添加省略号 */
const len = 142;
if (richText.length * 2 <= len) {
return richText;
}
/* 用于记录文字内容的总长度 */
let strLength = 0;
for (let i = 0; i < richText.length; i++) {
temporaryText = temporaryText + richText.charAt(i);
/* charCodeAt()返回指定位置的字符的Unicode编码,值为128以下时一个字符占一位,当值在128以上是一个字符占两位 */
if (richText.charCodeAt(i) > 128) {
strLength = strLength + 2;
if (strLength >= len) {
return temporaryText.substring(0, temporaryText.length - 1) + "...";
}
} else {
strLength = strLength + 1;
if (strLength >= len) {
return temporaryText.substring(0, temporaryText.length - 2) + "...";
}
}
}
return temporaryText;
}

2.vue中使用过滤器


filters: {
localData(value) {
let date = new Date(value * 1000);
let Month = date.getMonth() + 1;
let Day = date.getDate();
let Y = date.getFullYear() + '年';
let M = Month < 10 ? '0' + Month + '月' : Month + '月';
let D = Day + 1 < 10 ? '0' + Day + '日' : Day + '日';
let hours = date.getHours();
let minutes = date.getMinutes();
let hour = hours < 10 ? '0' + hours + ':' : hours + ':';
let minute = minutes < 10 ? '0' + minutes : minutes;
return Y + M + D + ' ' + hour + minute;
}
} /* 使用,直接在div中添加就可以了,| 前面的是参数,后面的是过滤器 */
<div class="time">{{data.etime | localData}}</div>

3.微信小程序中使用过滤器

  • 新建.wxs文件

var localData = function (value) {
var date = getDate(value * 1000);
var Month = date.getMonth() + 1;
var Day = date.getDate();
var hours = date.getHours(); //计算剩余的小时
var minutes = date.getMinutes(); //计算剩余的分钟
var Y = date.getFullYear() + '-';
var M = Month < 10 ? '0' + Month + '-' : Month + '-';
var D = Day + 1 < 10 ? '0' + Day + '' : Day + '';
var H = hours < 10 ? '0' + hours + ':' : hours + ':'
var m = minutes < 10 ? '0' + minutes : minutes;
return Y+M + D + " " + H + m;
}
module.exports = {
localData: localData
}
  • 使用,用<wxs />标签来引入,src为路径,module为引入的文件模块名

&lt;wxs src="./filters.wxs" module="tool" /&gt;
&lt;text class="scoreText"&gt;{{tool.filterScore(item.shop.score)}}分&lt;/text&gt;
  • 直接在.wxml文件中用<wxs></wxs>包裹

&lt;wxs module="foo"&gt;
var some_msg = "hello world";
module.exports = {
msg : some_msg,
}
&lt;/wxs&gt;
&lt;view&gt; {{foo.msg}} &lt;/view&gt;

4.react中使用

  • react中使用,其实就是定义一个方法

import noBanner from '@/assets/storeDetail/no-banner.jpg'
const filterImg = item =&gt; {
let bgImg;
if (item.shopimages == null) {
bgImg = noBanner;
} else {
bgImg = item.shopimages[0];
}
return bgImg;
};
/* 使用 */
&lt;img src={filterImg(storeitem)} className={style.topImg} alt="" /&gt;

正在努力学习中,若对你的学习有帮助,留下你的印记呗(点个赞咯^_^)

原文地址:https://segmentfault.com/a/1190000017075338

去除富文本中的html标签及vue、react、微信小程序中的过滤器的更多相关文章

  1. 在微信小程序中绘制图表(part2)

    本期大纲 1.确定纵坐标的范围并绘制 2.根据真实数据绘制折线 相关阅读:在微信小程序中绘制图表(part1)在微信小程序中绘制图表(part3) 关注我的 github 项目 查看完整代码. 确定纵 ...

  2. 微信小程序中显示html富文本的方法

    微信小程序中显示html富文本的方法 使用方法:git地址:https://github.com/icindy/wxParse 一.下载wxParse文件 二.在要引入的页面的js文件中,引入文件 j ...

  3. 在微信小程序中使用富文本转化插件wxParse

    在微信小程序中我们往往需要展示一些丰富的页面内容,包括图片.文本等,基本上要求能够解析常规的HTML最好,由于微信的视图标签和HTML标签不一样,但是也有相对应的关系,因此有人把HTML转换做成了一个 ...

  4. 微信小程序中转义字符的处理

    在微信小程序开发过程中,有时候会用到常用的一些特殊字符如:‘<’.‘>’.‘&’.‘空格’等,微信小程序同样支持对转义字符的处理,下面提供两种方法用来处理微信小程序中转义字符的处理 ...

  5. 全栈开发工程师微信小程序-中(下)

    全栈开发工程师微信小程序-中(下) 微信小程序视图层 wxml用于描述页面的结构,wxss用于描述页面的样式,组件用于视图的基本组成单元. // 绑定数据 index.wxml <view> ...

  6. 全栈开发工程师微信小程序-中(中)

    全栈开发工程师微信小程序-中(中) 开放能力 open-data 用于展示微信开放的数据 type 开放数据类型 open-gid 当 type="groupName" 时生效, ...

  7. 全栈开发工程师微信小程序-中

    全栈开发工程师微信小程序-中 多媒体及其他的组件 navigator 页面链接 target 在哪个目标上发生跳转,默认当前小程序,可选值self/miniProgram url 当前小程序内的跳转链 ...

  8. 微信小程序中this指向作用域问题this.setData is not a function报错

    在微信小程序中我们一般通过以下方式来修改data中的数据 this.setData({ index1: e.detail.value }) 比如在函数里面修改数据 bindFaChange1: fun ...

  9. 微信小程序中this关键字使用技巧

    转自:https://blog.csdn.net/qq_33956478/article/details/81348453 微信小程序中,在wx.request({});方法调用成功或者失败之后,有时 ...

随机推荐

  1. luoguP3203 [HNOI2010]BOUNCE 弹飞绵羊

    P3203 [HNOI2010]BOUNCE 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonke ...

  2. TCP连接数配置

    一般的设置规则为: 系统最大文件数>可用端口>用户最大文件数 sysctl -a | grep file-max cat /proc/sys/fs/file-max 这表明这台Linux系 ...

  3. TCP定时器 之 连接建立定时器

    当服务器收到新的syn请求,会回复syn+ack给请求端,若某时间内未收到请求端回复的ack,新建连接定时器超时执行回调,重传syn+ack,当超时超过固定次数时,该连接中止:本文主要分析其初始化流程 ...

  4. SpringBoot启动加载yml配置文件出现编码格式错误

    Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input l ...

  5. 开源EDR(OSSEC)基础篇- 02 -部署环境与安装方式

    https://yq.aliyun.com/articles/683077?spm=a2c4e.11163080.searchblog.9.753c2ec1lRj02l

  6. leetcode 63 不同路径II

    二维数组动态规划,还可以采用一维数组进行动态规划. class Solution { public: int uniquePathsWithObstacles(vector<vector< ...

  7. VS 2017 VC++项目出现 LNK1104 无法打开文件"libcmtd.lib" 的解决方法

    今天用VS 2017编译一个以前的VC++动态库项目,出现了一个链接器问题: LNK1104 无法打开文件"libcmtd.lib" . 操作系统版本为:Windows 10 18 ...

  8. 【翻译】WPF应用程序模块化开发快速入门(使用Prism+MEF)

    编译并运行快速入门 需要在VisualStudio 2010上运行此快速入门示例 代码下载:ModularityWithMef.zip 先重新生成解决方案 再按F5运行此示例 说明: 在此快速入门示例 ...

  9. jqGrid整理笔记

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. 修改Windows 2008以后系统的NTP服务设置

    @echo off echo autor OAK @echo off echo -------------------------------- @echo off echo setup time r ...