diff --git a/snippets/escapeString.md b/snippets/escapeString.md new file mode 100644 index 000000000..a1bf2fc7f --- /dev/null +++ b/snippets/escapeString.md @@ -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, '>'); +``` + +```js +escapeString('Me & you'); // '<a href="#">Me & you</a>' +``` diff --git a/snippets/unescapeString.md b/snippets/unescapeString.md new file mode 100644 index 000000000..5e6e14a01 --- /dev/null +++ b/snippets/unescapeString.md @@ -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>'); // 'Me & you' +``` diff --git a/tag_database b/tag_database index 5c3a9153b..e8874cf82 100644 --- a/tag_database +++ b/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