diff --git a/README.md b/README.md index d350e2006..f3127ec90 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,7 @@ * [`capitalize`](#capitalize) * [`capitalizeEveryWord`](#capitalizeeveryword) * [`countVowels`](#countvowels) +* [`escapeHTML`](#escapehtml) * [`escapeRegExp`](#escaperegexp) * [`fromCamelCase`](#fromcamelcase) * [`repeatString`](#repeatstring) @@ -226,6 +227,7 @@ * [`toKebabCase`](#tokebabcase) * [`toSnakeCase`](#tosnakecase) * [`truncateString`](#truncatestring) +* [`unescapeHTML`](#unescapehtml) * [`words`](#words) @@ -3276,6 +3278,39 @@ countVowels('gym'); // 0
[⬆ Back to top](#table-of-contents) +### 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) + ); +``` + +
+Examples + +```js +escapeHTML('Me & you'); // '<a href="#">Me & you</a>' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### escapeRegExp Escapes a string to use in a regular expression. @@ -3551,6 +3586,38 @@ truncateString('boomerang', 7); // 'boom...'
[⬆ Back to top](#table-of-contents) +### 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) + ); +``` +
+Examples + +```js +unescapeHTML('<a href="#">Me & you</a>'); // 'Me & you' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### words Converts a given string into an array of words. diff --git a/docs/index.html b/docs/index.html index cda31a86a..bb72b7b1f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -237,6 +237,7 @@ capitalize capitalizeEveryWord countVowels +escapeHTML escapeRegExp fromCamelCase repeatString @@ -247,6 +248,7 @@ toKebabCase toSnakeCase truncateString +unescapeHTML words

Utility @@ -1516,6 +1518,24 @@ capitalize('fooBar', true); // 'Foobar'
countVowels('foobar'); // 3
 countVowels('gym'); // 0
 
+

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).

+
const escapeHTML = str =>
+  str.replace(
+    /[&<>'"]/g,
+    tag =>
+      ({
+        '&': '&amp;',
+        '<': '&lt;',
+        '>': '&gt;',
+        "'": '&#39;',
+        '"': '&quot;'
+      }[tag] || tag)
+  );
+
+
escapeHTML('<a href="#">Me & you</a>'); // '&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;'
+

escapeRegExp

Escapes a string to use in a regular expression.

Use replace() to escape special characters.

@@ -1641,6 +1661,24 @@ Return the string truncated to the desired length, with ... appende
truncateString('boomerang', 7); // 'boom...'
 
+

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).

+
const unescapeHTML = str =>
+  str.replace(
+    /&amp;|&lt;|&gt;|&#39;|&quot;/g,
+    tag =>
+      ({
+        '&amp;': '&',
+        '&lt;': '<',
+        '&gt;': '>',
+        '&#39;': "'",
+        '&quot;': '"'
+      }[tag] || tag)
+  );
+
+
unescapeHTML('&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;'); // '<a href="#">Me & you</a>'
+

words

Converts a given string into an array of words.

Use String.split() with a supplied pattern (defaults to non-alpha as a regex) to convert to an array of strings. Use Array.filter() to remove any empty strings. diff --git a/snippets/escapeHTML.md b/snippets/escapeHTML.md index 7ca23af6d..2db4cf0c0 100644 --- a/snippets/escapeHTML.md +++ b/snippets/escapeHTML.md @@ -5,13 +5,18 @@ 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); +const escapeHTML = str => + str.replace( + /[&<>'"]/g, + tag => + ({ + '&': '&', + '<': '<', + '>': '>', + "'": ''', + '"': '"' + }[tag] || tag) + ); ``` ```js diff --git a/snippets/unescapeHTML.md b/snippets/unescapeHTML.md index 3325e1400..d3d43b827 100644 --- a/snippets/unescapeHTML.md +++ b/snippets/unescapeHTML.md @@ -5,13 +5,19 @@ 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);``` +const unescapeHTML = str => + str.replace( + /&|<|>|'|"/g, + tag => + ({ + '&': '&', + '<': '<', + '>': '>', + ''': "'", + '"': '"' + }[tag] || tag) + ); +``` ```js unescapeHTML('<a href="#">Me & you</a>'); // 'Me & you' ``` diff --git a/tag_database b/tag_database index 604d1d876..d0a06ba0d 100644 --- a/tag_database +++ b/tag_database @@ -29,8 +29,8 @@ distinctValuesOfArray:array dropElements:array dropRight:array elementIsVisibleInViewport:browser -escapeRegExp:string escapeHTML:string +escapeRegExp:string everyNth:array extendHex:utility factorial:math