My flag doesn't seem to work

Tell us what’s happening:
Hi,
My regex is matching just the first match. However, in my debugger regex value is /&/g (when "Dolce & Gabbana"). So, I was hoping as much matches as existing in (in "Dolce & & Gabbana" should be 2 to be replaced). I also tried with a function as 2nd parameter (bottom): same results. What am I missing guys?

Your code so far


function convertHTML(str) {

 const reservedChars = ["&","<",">", "","'"];
 const dictionaryOfEntities = {
   "&": "&amp;",
   "<": "&lt;",
   ">": "&gt;",
   '"': "&quot;",
   "'": "&apos;",
 }

let reserved = str.split('').filter(char => reservedChars.indexOf(char) !== -1)

let regex = new RegExp (`${reserved}`,'g')
 if (reserved.length === 0) return str
 else return str.replace(regex, dictionaryOfEntities[reserved])
}

console.log(convertHTML("Dolce & Gabbana"));//-> "Dolce &amp; Gabbana"
console.log(convertHTML("Dolce & & Gabbana"));//-> "Dolce & & Gabbana"
function convertHTML(str) {

  const reservedChars = ["&","<",">", "","'"];
  const dictionaryOfEntities = {
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;",
    '"': "&quot;",
    "'": "&apos;",
  }

let reserved = str.split('').filter(char => reservedChars.indexOf(char) !== -1)

let regex = new RegExp (`${reserved}`,'g')
  if (reserved.length === 0) return str
  else return str.replace(regex, entity)

  function entity (match){
  return match.replace(match, dictionaryOfEntities[reserved])
}  
}

console.log(convertHTML("Dolce & Gabbana"));//-> "Dolce &amp; Gabbana"
console.log(convertHTML("Dolce & & Gabbana"));//-> "Dolce & & Gabbana"

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 OPR/67.0.3575.115.

Challenge: Convert HTML Entities

Link to the challenge:

the issue is that reserved is an array, so if you have more than one element in there, you get a comma separated list, so (for example) this: /&,&/g, so it will match only ampersand-comma-ampersand

you need to change approach

1 Like

Ah, that’s a typo. it is """

I used Set to avoid duplicates, but it doesn’t work with "<>" cause what you explained

I gonna try it and of course it’m gonna work (just like chaining multiple times replace functions). It 's just that I wanted to accomplish a function that rests exclusively on the input that we don’t know in advance. But I guess it is not possible in this case.

Edit: I was mulling over and this is no sense cause in anyways I’m getting the replace from my dictionaryOfEntities, so if the entity bound to an x input is not there, the program isn’t going to give the needed output when x.

Well guys, thanks for the advice. This my solution

function convertHTML(str) {
  const reservedChars = ["&","<",">",'"',"'"];
  let reserved = str.split('').filter(char =>
  reservedChars.indexOf(char) !== -1)

  if (reserved.length === 0) return str
  else return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;")
}

convertHTML('Stuff in "quotation marks"');