首页 > 代码库 > Convert HTML Entities

Convert HTML Entities

function convert(str) {
  // :)
  //return str;
  var HTML_Entities = {
    ‘&‘:‘&‘,
    ‘<‘:‘<‘,
    ‘>‘:‘>‘,
    ‘"‘:‘"‘,
    "‘":‘‘‘
  };
  var regExp = /[&<>"‘]/g;
  var arr = str.match(regExp);
  if(!arr){
    return str;
  }else{
    for(var i = 0; i < arr.length; i++){
      str = str.replace(arr[i],HTML_Entities[arr[i]]);
    }
    return str;
  }
}

convert("Dolce & Gabbana");

 

Convert HTML Entities