Convert HTML Entities - making it smarter?

Convert HTML Entities - making it smarter?
0

#1

I have a solution to the Convert HTML entities challenge - it is similar to other ones. But what I want to do is make it work correctly if the input string already has an HTML entitiy. For example “Dolce & Gabbana” shouldn’t try to replace &.

Anyone have any ideas about how to do this? Thanks

Your code so far

function convertHTML(str) {
  var tbl  = {
    '&' : '&', 
    '<' : '&lt;', 
    '>' : '&gt;', 
    '"'  : '&quot;', 
     "'" : '&apos;'
};

  return str.replace (/[&<>"']/g, function(matched) {
    return tbl[matched];
  });
}

convertHTML("Dolce & Gabbana");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:


#2

oops - burned by editor! If input string already has “&amp;”, then I don’t want to replace the ampersand