js html 特殊字元轉義

bei_fang_du_lang發表於2017-02-21
function html_encode(str)   
{   
  var s = "";   
  if (str.length == 0) return "";   
  s = str.replace(/&/g, "&");   
  s = s.replace(/</g, "<");   
  s = s.replace(/>/g, ">");   
  s = s.replace(/ /g, " ");   
  s = s.replace(/\'/g, "'");   
  s = s.replace(/\"/g, """);   
  s = s.replace(/\n/g, "<br>");   
  return s;   
} 

相關文章