zoukankan      html  css  js  c++  java
  • javascript特殊字符过滤函数

    <script type="text/javascript">
    <!--

    var str = '<ok>,-';

    // 用Hash存储进行正则替换
    String.prototype.multiReplace = function(hash){
        
    var str = this, key;
        
    for(key in hash){
            
    if(Object.prototype.hasOwnProperty.call(hash, key)){
                str 
    = str.replace(new RegExp( key, 'g'), hash[key]);
            }
        }
        
    return str;
    }
    str 
    = str.multiReplace({
        
    '&(?!#?\\w+;)' : '&amp;'   ,
        
    '"([^"]*)"'    : '“$1”'    ,
        
    '<'            : '&lt;'    ,
        
    '>'            : '&gt;'    ,
        
    ''            : '&hellip;',
        
    ''            : '&ldquo;' ,
        
    ''            : '&rdquo;' ,
        
    ''            : '&lsquo;' ,
        
    ''            : '&rsquo;' ,
        
    ''            : '&mdash;' ,
        
    ''            : '&ndash;'
    });
    alert(str);

    //-->
    </script>

    运用Hash结构存放 方便操作

  • 相关阅读:
    8.16
    8.6 总结
    Educational Codeforces Round 45 (Rated for Div. 2)
    Codeforces Round #487 (Div. 2)
    Codeforces Round #485
    codeforces Avito Code Challenge 2018
    MySQL索引知识面试题
    使用多线程优化复杂逻辑以及数据量多处理
    elasticsearch 和Ik 以及 logstash 同步数据库数据
    linux 安装elasticsearch步骤以及入的坑
  • 原文地址:https://www.cnblogs.com/naoguazi/p/1771933.html
Copyright © 2011-2022 走看看