JS对HTML字符的转义

function htmlEscape(str){ 
    var s = "";
    if(str.length == 0) return "";
    s = str.replace(/&/g,"&");
    s = s.replace(/</g,"&lt;");
    s = s.replace(/>/g,"&gt;");
    s = s.replace(/ /g,"&nbsp;");
    s = s.replace(/\'/g,"&#39;");
    s = s.replace(/\"/g,"&quot;");
    return s;  
}

 

posted @ 2019-04-04 14:46  岁月淡忘了谁  阅读(411)  评论(0编辑  收藏  举报