Last active
August 29, 2015 14:24
-
-
Save placidrod/decf4140039d5dd0167e to your computer and use it in GitHub Desktop.
FCC Bonfire: Convert HTML Entities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convert(str) { | |
var ent = { | |
'&' : '&', | |
'<' : '<', | |
'>' : '>', | |
'\"': '"', | |
"\'": ''' | |
}; | |
var re = new RegExp(Object.keys(ent).join('|'), "g"); | |
console.log(re); | |
str = str.replace(re,function(match){ | |
return ent[match]; | |
}); | |
return (str); | |
} | |
// convert('Dolce & Gabbana&'); | |
// convert('<>'); | |
// convert('Hamburgers < Pizza < Tacos'); | |
// convert('Stuff in "quotation marks"'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment