Search code examples
javascriptstringformatting

How do I convert special characters like: " into chracters like: " with JS?


I found these string things (idk what to call them) from a json database and I noticed that it was writing those letters instead of the actual character. I undestand why, but I can't figure out how to convert it to a normal character. I have tried searching online but I didn't find anything.

Another of me seeing this is instead of ', I get

'

I tried searching online and through Stack Overflow and I couldn't find a solution to my problem.


Solution

  • Those things are called HTML entities. Here's kind of a hcky solution to convert them using a helper element:

    const helper = document.createElement("div")
    helper.innerHTML = "potato&tomato" // set HTML content with entity
    console.log(helper.innerText) // read "rendered" content