Rename unescapeString.md to unescapeHTML.md

This commit is contained in:
atomiks
2017-12-30 00:09:10 +11:00
committed by GitHub
parent 8433f0e445
commit 05f5e3c9c9

17
snippets/unescapeHTML.md Normal file
View File

@ -0,0 +1,17 @@
### unescapeHTML
Unescapes escaped HTML characters.
Use `String.replace()` with a regex that matches the characters that need to be escaped, using a callback function to replace each escaped character instance with its associated unescaped character using a dictionary (object).
```js
const unescapeHTML = str => str.replace(/[&<>'"]/g, tag => ({
'&amp;': '&',
'&lt;': '<',
'&gt;': '>',
'&#39;': '\'',
'&quot;': '"'
})[tag] || tag);```
```js
unescapeHTML('&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;'); // '<a href="#">Me & you</a>'
```