Add escape/unescape string
This commit is contained in:
14
snippets/escapeString.md
Normal file
14
snippets/escapeString.md
Normal file
@ -0,0 +1,14 @@
|
||||
### escapeString
|
||||
|
||||
Escapes a string for use in HTML.
|
||||
|
||||
Use a chain of `String.replace()` calls combined with regular expressions to replace special characters with the proper symbols.
|
||||
|
||||
```js
|
||||
const escapeString = str =>
|
||||
str.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
|
||||
```
|
||||
|
||||
```js
|
||||
escapeString('<a href="#">Me & you</a>'); // '<a href="#">Me & you</a>'
|
||||
```
|
||||
14
snippets/unescapeString.md
Normal file
14
snippets/unescapeString.md
Normal file
@ -0,0 +1,14 @@
|
||||
### unescapeString
|
||||
|
||||
Unescapes a string from HTML.
|
||||
|
||||
Use a chain of `String.replace()` calls combined with regular expressions to replace special characters with the proper symbols.
|
||||
|
||||
```js
|
||||
const unescapeString = str =>
|
||||
str.replace(/>/g, '>').replace(/</g, '<').replace(/'/g, '\'').replace(/"/g, '"').replace(/&/g, '&');
|
||||
```
|
||||
|
||||
```js
|
||||
unescapeString('<a href="#">Me & you</a>'); // '<a href="#">Me & you</a>'
|
||||
```
|
||||
10
tag_database
10
tag_database
@ -1,6 +1,6 @@
|
||||
anagrams:string
|
||||
arrayToHtmlList:browser
|
||||
average:uncategorized
|
||||
average:math
|
||||
bottomVisible:browser
|
||||
call:adapter
|
||||
capitalize:string
|
||||
@ -30,6 +30,7 @@ dropElements:array
|
||||
dropRight:array
|
||||
elementIsVisibleInViewport:browser
|
||||
escapeRegExp:string
|
||||
escapeString:string
|
||||
everyNth:array
|
||||
extendHex:utility
|
||||
factorial:math
|
||||
@ -76,9 +77,9 @@ JSONToFile:node
|
||||
last:array
|
||||
lcm:math
|
||||
mapObject:array
|
||||
max:uncategorized
|
||||
max:math
|
||||
median:math
|
||||
min:uncategorized
|
||||
min:math
|
||||
negate:logic
|
||||
nthElement:array
|
||||
objectFromPairs:object
|
||||
@ -120,7 +121,7 @@ sortCharactersInString:string
|
||||
speechSynthesis:media
|
||||
spreadOver:adapter
|
||||
standardDeviation:math
|
||||
sum:uncategorized
|
||||
sum:math
|
||||
symmetricDifference:array
|
||||
tail:array
|
||||
take:array
|
||||
@ -136,6 +137,7 @@ toOrdinalSuffix:utility
|
||||
toSnakeCase:string
|
||||
truncateString:string
|
||||
truthCheckCollection:object
|
||||
unescapeString:string
|
||||
union:array
|
||||
UUIDGeneratorBrowser:browser
|
||||
UUIDGeneratorNode:node
|
||||
|
||||
Reference in New Issue
Block a user