Rename unescapeString.md to unescapeHTML.md
This commit is contained in:
17
snippets/unescapeHTML.md
Normal file
17
snippets/unescapeHTML.md
Normal 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 => ({
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
''': '\'',
|
||||
'"': '"'
|
||||
})[tag] || tag);```
|
||||
```js
|
||||
unescapeHTML('<a href="#">Me & you</a>'); // '<a href="#">Me & you</a>'
|
||||
```
|
||||
Reference in New Issue
Block a user