Skip to content

Instantly share code, notes, and snippets.

@jepras
Created October 1, 2018 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepras/0d6b9487e7f9f98f784dd8d537c20c50 to your computer and use it in GitHub Desktop.
Save jepras/0d6b9487e7f9f98f784dd8d537c20c50 to your computer and use it in GitHub Desktop.
Use switch to replace chars in a string. Use \ to escape characters.
function convertHTML(str) {
var htmlStr = "";
var search = function(char) {
switch (char) {
case '&':
console.log("& found");
str = str.replace("&", "&");
break;
case '<':
console.log("< found");
str = str.replace("<", "&lt;");
break;
case '>':
console.log("> found");
str = str.replace(">", "&gt;");
break;
case '\"':
console.log("double quotes found");
str = str.replace("\"", "&quot;");
break;
case "'":
console.log("quote found");
str = str.replace("'", "&apos;");
break;
}
}
for (var i = 0; i < str.length; i++) {
search(str[i]);
}
return str;
}
convertHTML("Schindler's List");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment