Native JavaScript or ES6 way to encode and decode HTML objects?

Is there any way to encode or decode HTML objects using JavaScript or ES6? For example, < will be encoded as &lt; . There are libraries like html-entities for Node.js, but it looks like there should be something built into JavaScript in JavaScript that already addresses this common need.

+14
javascript html-entities
source share
4 answers

There is no built-in function in the JavaScript API that converts ASCII characters to the equivalent of "html-entity". Here is the beginning of the solution and a simple trick you might like

+3
source share

A nice feature using es6 to exit HTML:

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

To decode html, I just use the <pre> and this function:

 function convertHTML(str) { var entityPairs = [ {character: '&', html: '&amp;'}, {character: '<', html: '&lt;'}, {character: '>', html: '&gt;'}, {character: "'", html: '&apos;'}, {character: '"', html: '&quot;'}, ]; entityPairs.forEach(function(pair){ var reg = new RegExp(pair.character, 'g'); str = str.replace(reg, pair.html); }); return str; } //var or file.html from Ajax var str = ` <head> <title>OK</title> </head>`; document.getElementById("preHtml").innerHTML = convertHTML(str); 
 <pre id="preHtml"></pre> 
0
source share

Throw your own (caveat - use HE instead for most use cases)

For pure JS without a library, you can encode and decode HTML objects using pure Javascript , for example:

 let encode = str => { let buf = []; for (var i = str.length - 1; i >= 0; i--) { buf.unshift(['&#', str[i].charCodeAt(), ';'].join('')); } return buf.join(''); } let decode = str => { return str.replace(/&#(\d+);/g, function(match, dec) { return String.fromCharCode(dec); }); } 

Usages :

 encode("Hello > ยฉ <") // "&#72;&#101;&#108;&#108;&#111;&#32;&#62;&#32;&#169;&#32;&#60;" decode("Hello &gt; &copy; &#169; &lt;") // "Hello &gt; &copy; ยฉ &lt;" 

However, you can see that this approach has a couple of disadvantages:


Use the HE library (HTML objects)

Usage :

 he.encode('foo ยฉ bar โ‰  baz ๐Œ† qux'); // Output : 'foo &#xA9; bar &#x2260; baz &#x1D306; qux' he.decode('foo &copy; bar &ne; baz &#x1D306; qux'); // Output : 'foo ยฉ bar โ‰  baz ๐Œ† qux' 

Related issues

  • How to convert characters to HTML objects using simple JavaScript
  • Encode HTML entities in JavaScript
  • Unescape HTML objects in javascript?
  • HTML Entity Decode
  • How to decode a string containing special HTML objects?
  • Remove HTML from text JavaScript
0
source share

All Articles