Intermediate Algorithm Scripting - Convert HTML Entities

Tell us what’s happening:
My solution passes all tests, but I don’t understand str = str.replace(/^['"]/,"");.
Why does this line remove both beginning and ending " or '?
The output of 'Stuff in "quotation marks"'.replace(/^['"]/,"") is Stuff in "quotation marks"

My original solution to remove beginning and ending quotes once is str.replace(/^['"]/,"").replace(/['"]$/,"")

Your code so far

Summary

This text will be hidden

function convertHTML(str) {
  const html = {
    "&": "&",
    "<": "&lt;",
    ">": "&gt;",
    "\"": "&quot;",
    "'": "&apos;"
  };
  str = str.replace(/^['"]/,"");
  console.log(str);
  for (let char in html) {
    let regex = new RegExp(char,"g");
    str = str.replace(regex, html[char]);
  }
  return str;
}
console.log(convertHTML("Schindler's List"));
// convertHTML("Dolce & Gabbana");

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Convert HTML Entities

Link to the challenge:

1 Like

Can you explain why you want to remove quotes from the strings? It doesn’t look like that’s a requirement.

Because outer quotes (either ’ or ") will be omitted, and inner quotes get translated to html entities like &quot and &apos, based on my observation on test cases. My approach is likely suboptimal. Please let me know if I am doing it wrong or there’s better approach.

There are no outer quote characters in the string itself, the instructions are just denoting that you will receive strings by wrapping them in quotes.

So, you can just delete that line entirely.

2 Likes

omg :upside_down_face:, you are absolutely right…what was I thinking…

Don’t beat yourself up! It happens lol

2 Likes