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

1.富文本去除html标签

  • 去除html标签及 空格

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

  1. content = formatRichText(content);
  2. console.log(content);
  3. /* 使用substring来截取字符串 */
  4. if (content.length > 10) {
  5. content = content.substring(0, 10) + '...';
  6. }
  7. console.log(content);
  8. /* 限制字数后添加省略号 */
  9. function formatRichText(richText) {
  10. let temporaryText = '';
  11. /* 设置多长后添加省略号 */
  12. const len = 142;
  13. if (richText.length * 2 <= len) {
  14. return richText;
  15. }
  16. /* 用于记录文字内容的总长度 */
  17. let strLength = 0;
  18. for (let i = 0; i < richText.length; i++) {
  19. temporaryText = temporaryText + richText.charAt(i);
  20. /* charCodeAt()返回指定位置的字符的Unicode编码,值为128以下时一个字符占一位,当值在128以上是一个字符占两位 */
  21. if (richText.charCodeAt(i) > 128) {
  22. strLength = strLength + 2;
  23. if (strLength >= len) {
  24. return temporaryText.substring(0, temporaryText.length - 1) + "...";
  25. }
  26. } else {
  27. strLength = strLength + 1;
  28. if (strLength >= len) {
  29. return temporaryText.substring(0, temporaryText.length - 2) + "...";
  30. }
  31. }
  32. }
  33. return temporaryText;
  34. }

2.vue中使用过滤器


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

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

  • 新建.wxs文件

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

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

  1. &lt;wxs module="foo"&gt;
  2. var some_msg = "hello world";
  3. module.exports = {
  4. msg : some_msg,
  5. }
  6. &lt;/wxs&gt;
  7. &lt;view&gt; {{foo.msg}} &lt;/view&gt;

4.react中使用

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

  1. import noBanner from '@/assets/storeDetail/no-banner.jpg'
  2. const filterImg = item =&gt; {
  3. let bgImg;
  4. if (item.shopimages == null) {
  5. bgImg = noBanner;
  6. } else {
  7. bgImg = item.shopimages[0];
  8. }
  9. return bgImg;
  10. };
  11. /* 使用 */
  12. &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. luoguP1034 矩形覆盖 x

    P1034 矩形覆盖 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4( ...

  2. 【BZOJ3261】最大异或和(可持久化Trie)

    题意: 思路:可持久化Trie板子题,支持序列插入和询问 #include<bits/stdc++.h> using namespace std; typedef long long ll ...

  3. 使用svn在github上下载文件夹

    今天想在github上下载mybatis-generator的eclipse插件,可是如何在github上下载一个文件夹而不用把这个项目clone呢,搜了一下,发现可以直接用svn来下载 只需将将路径 ...

  4. Super Mario(主席树)

    Super Mario  Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded ...

  5. PTA 刷题与Z老师的头发

    刷题与Z老师的头发 (10 分) 在Pintia上,每天Z老师出题.小盆友们刷题.Z老师的头发遵从以下规律: 1.每天生长出60根头发: 2.每出一道题,减少20根头发: 3.每天结束时统计累积做题情 ...

  6. Unity3D_(游戏)卡牌02_主菜单界面

      启动屏界面.主菜单界面.选关界面.游戏界面 卡牌01_启动屏界面 传送门 卡牌02_主菜单界面 传送门 卡牌03_选关界面 传送门 卡牌04_游戏界面    传送门 主菜单界面 (选择左边图标或选 ...

  7. DVWA--File Inclusion(不能远程包含的问题解决)

    然后别以为这样就完了 我被这样坑了一下午 找到你对应版本的php 进去Ctrl+f 搜索url_allow——fopen 和include

  8. python学习之路(18)

    返回函数 函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个可变参数的求和.通常情况下,求和的函数是这样定义的: >>> def a(* ...

  9. 消息队列rabbitmq/kafka

    12.1 rabbitMQ 1. 你了解的消息队列 rabbitmq是一个消息代理,它接收和转发消息,可以理解为是生活的邮局.你可以将邮件放在邮箱里,你可以确定有邮递员会发送邮件给收件人.概括:rab ...

  10. C++入门经典-例3.3-if-else语句的奇偶性判别

    1:代码如下: // 3.3.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...