782 B
782 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | ||
|---|---|---|---|---|---|---|---|
| Unescape HTML | snippet | javascript |
|
little-tree | 2020-10-22T20:24:44+03:00 |
Unescapes escaped HTML characters.
- Use
String.prototype.replace()with a regexp that matches the characters that need to be unescaped. - Use the function's callback to replace each escaped character instance with its associated unescaped character using a dictionary (object).
const unescapeHTML = str =>
str.replace(
/&|<|>|'|"/g,
tag =>
({
'&': '&',
'<': '<',
'>': '>',
''': "'",
'"': '"'
}[tag] || tag)
);
unescapeHTML('<a href="#">Me & you</a>');
// '<a href="#">Me & you</a>'