zoukankan      html  css  js  c++  java
  • 常见富文本处理方法

    // html特殊字符转义
    const special = html => {
        html = html.replace(/ /gm, ' ');
        html = html.replace(/&lt;/gm, '<');
        html = html.replace(/&gt;/gm, '>');
        html = html.replace(/&Oslash;/gm, 'Ø');
        html = html.replace(/&deg;/gm, '°');
        html = html.replace(/&quot;/gm, '"');
        html = html.replace(/&middot;/gm, '·');
        html = html.replace(/&amp;/gm, '&');
        html = html.replace(/&yen;/gm, '¥');
        return html;
    };
    
    // 移除样式字段
    const removeCSS = html => {
        html = html.replace(/style=["'][^"']*['"]/ig, '');
        html = html.replace(/<style>[^<]*</style>/ig, '');
        return html;
    };
    
    // 替换标签
    const stripHTML = html => {
        html = removeCSS(html);
        html = html.replace(/\n|
    /gm, '<br>');
        html = html.replace(/(</p>)|(<tr>)|(</table>)|(<ol>)|(</ol>)|(<ul>)|(</ul>)|(<div>)|(</div>)/gm, '<br>');
        html = html.replace(/(<b[s]*>)|(</b[s]*>)/ig, '');
        html = html.replace(/(<li>)/gm, '');
        html = special(html);
        let regex = /<[^img | ^br].*?>/ig;
    
        html = html.replace(regex, '');
        html = html.replace(/(( *
     *
    s*)+)/gm, '');
        html = html.trim();
        html = html.replace(/(<br[s]*[/]?>[s]*)+/ig, '<br>');
        html = html.replace(/(^<br>)|(<br>$)/ig, '');
        return html;
    };
    
  • 相关阅读:
    Windows常用命令
    路由器命令基础使用
    《计算机网络》-CCNA命令大全
    Cisco 2960交换机配置
    vscode使用技巧
    Luogu 3321 [SDOI2015]序列统计
    Luogu 3702 [SDOI2017]序列计数
    CF 990 Educational Codeforces Round 45
    Luogu 4705 玩游戏
    CF 438E The Child and Binary Tree
  • 原文地址:https://www.cnblogs.com/ljwk/p/11905859.html
Copyright © 2011-2022 走看看