zoukankan      html  css  js  c++  java
  • HTML和实体相互转换

    // HTML和实体相互转换 
    String.prototype.convertEntity=(function(){
    // 字符实体表
    let entity = {
    quot : '"',
    lt : '<',
    gt : '>',
    amp : '&',
    nbsp : ' '
    }
    let entity_cover_key='',entity_cover={};
    for(let item in entity){
    entity_cover_key+=entity[item];
    entity_cover[entity[item]]='&'+item+';';
    }
    return function(type){
    if(type == 1){
    return this.replace(/&([^&;]+);/g,function(a,b){
    return typeof entity[b] === 'string' ? entity[b] : a;
    })
    }else{
    let reg=new RegExp('['+entity_cover_key+']','g') 
    return this.replace(reg,function(c){
    return entity_cover[c];
    });
    }
    }
    })()
    
    console.log('&quot;&gt;&lt;&amp;&nbsp;&asds;'.covert(1)); // ""><& "
    console.log('<div>121212</div>'.covert()); // &lt;div&gt;121212&lt;/div&gt;
  • 相关阅读:
    2.8
    2.7
    2.6
    2.5
    2.4第三篇读后感
    2.2第一篇读后感
    2.1
    字符统计
    6468: Snuke's Coloring
    6463: Tak and Hotels II
  • 原文地址:https://www.cnblogs.com/yuesu/p/10734341.html
Copyright © 2011-2022 走看看