How can I modify the code below to properly escape HTML and strings in JavaScript arrays? I've been attempting to use a function called escapeHtml
to add the necessary slashes, but it's not functioning as expected. Any help would be appreciated. Thanks!
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function escapeHtml(unsafe)
{
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
var unsafe = ["car's", "<h1>hello</h1>", "Ref#1", "a-b", "he said "hello" to him"];
document.getElementById("demo").innerHTML = escapeHtml(unsafe);
</script>
</body>
</html>