Merge pull request #388 from Chalarangelo/escape-unescape-string
[FEATURE] Add escape/unescape string
This commit is contained in:
19
snippets/escapeHTML.md
Normal file
19
snippets/escapeHTML.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
### escapeHTML
|
||||||
|
|
||||||
|
Escapes a string for use in HTML.
|
||||||
|
|
||||||
|
Use `String.replace()` with a regex that matches the characters that need to be escaped, using a callback function to replace each character instance with its associated escaped character using a dictionary (object).
|
||||||
|
|
||||||
|
```js
|
||||||
|
const escapeHTML = str => str.replace(/[&<>'"]/g, tag => ({
|
||||||
|
'&': '&',
|
||||||
|
'<': '<',
|
||||||
|
'>': '>',
|
||||||
|
'\'': ''',
|
||||||
|
'"': '"'
|
||||||
|
})[tag] || tag);
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
escapeHTML('<a href="#">Me & you</a>'); // '<a href="#">Me & you</a>'
|
||||||
|
```
|
||||||
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>'
|
||||||
|
```
|
||||||
@ -30,6 +30,7 @@ dropElements:array
|
|||||||
dropRight:array
|
dropRight:array
|
||||||
elementIsVisibleInViewport:browser
|
elementIsVisibleInViewport:browser
|
||||||
escapeRegExp:string
|
escapeRegExp:string
|
||||||
|
escapeHTML:string
|
||||||
everyNth:array
|
everyNth:array
|
||||||
extendHex:utility
|
extendHex:utility
|
||||||
factorial:math
|
factorial:math
|
||||||
@ -137,6 +138,7 @@ toOrdinalSuffix:utility
|
|||||||
toSnakeCase:string
|
toSnakeCase:string
|
||||||
truncateString:string
|
truncateString:string
|
||||||
truthCheckCollection:object
|
truthCheckCollection:object
|
||||||
|
unescapeHTML:string
|
||||||
union:array
|
union:array
|
||||||
UUIDGeneratorBrowser:browser
|
UUIDGeneratorBrowser:browser
|
||||||
UUIDGeneratorNode:node
|
UUIDGeneratorNode:node
|
||||||
|
|||||||
Reference in New Issue
Block a user