Skip to content

Instantly share code, notes, and snippets.

@JohnnyR030T
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save JohnnyR030T/8ababc280b9e462e5867 to your computer and use it in GitHub Desktop.

Select an option

Save JohnnyR030T/8ababc280b9e462e5867 to your computer and use it in GitHub Desktop.
Bonfire: Convert HTML Entities
// Convert the characters "&", "<", ">", '"' (double quote), and "'" (apostrophe), in a string to their corresponding HTML entities.
function convert(str) {
if (str.includes('&')){
str = str.replace('&', '&amp');
}
else if (str.includes('<')) {
str = str.replace('<', '&lt');
}
else if (str.includes('>')) {
str = str.replace('>', '&gt');
}
else if (str.includes('"'))
str = str.replace("'", "&apos");
return str;
}
convert('Dolce & Gabbana');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment