Prepare repository for merge
This commit is contained in:
32
javascript/snippets/unescape-html.md
Normal file
32
javascript/snippets/unescape-html.md
Normal file
@ -0,0 +1,32 @@
|
||||
---
|
||||
title: Unescape HTML
|
||||
type: snippet
|
||||
tags: [string,regexp]
|
||||
cover: little-tree
|
||||
dateModified: 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).
|
||||
|
||||
```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