HomeLinuxWhat’s the Proper Solution to Decode a String That has Particular HTML...

What’s the Proper Solution to Decode a String That has Particular HTML Entities in it?


Whereas working with HTML, it’s potential to come across particular characters or symbols which were encoded utilizing HTML entities. These entities start with an ampersand “&” and terminate with a semicolon “;”, equivalent to “&lt;” which signifies the image “<”. You will need to exclude particular HTML components/entities from a string to confirm that the ultimate string is secure to make use of and accommodates no unlawful code that the browser might execute.

This submit will let you realize the suitable method for decoding strings with particular HTML entities.

What’s the Proper Solution to Decode a String That has Particular HTML Entities in it?

To decode a string that accommodates particular HTML entities in it, use the next strategies:

Technique 1: Decode a String That has Particular HTML Entities in it Utilizing “textarea” Component

Use the HTML “<textarea>” factor for decoding a string that accommodates particular HTML entities. It takes a string with particular HTML entities utilizing the “innerHTML” property. The browser routinely decodes the entities within the textarea and provides the straightforward plain textual content. For retrieving the decoded string, use the “worth” property.

Instance

Create a variable “encodedString” that shops a string containing particular HTML entities in it:

const encodedString = ‘&lt;div&gt;Welcome to Linuxhint!&lt;/div&gt;’;

Print the encoded string on the console:

console.log(“Encoded String: “ + encodedString);

Create an HTML factor “textarea” utilizing the “createElement()” methodology:

const textarea = doc.createElement(‘textarea’);

Move the encoded string to the textarea utilizing the “innerHTML” property:

textarea.innerHTML = encodedString;

Now, get the decoded string utilizing the “worth” attribute of the textarea and retailer it in a variable “decodedString”:

const decodedString = textarea.worth;

Lastly, show the decoded string on the console utilizing the “console.log()” methodology:

console.log(“Decoded String: “ + decodedString);

The output signifies that the string containing particular HTML entities has been efficiently decoded:

The above strategy is easy and clear, and it’s appropriate for easy situations. In the event you attempt to deal with advanced HTML buildings, it would fail. So, for that, use the “parseFromString()” Technique.

Technique 2: Decode a String That has Particular HTML Entities in it Utilizing “parseFromString()” Technique

One other technique to decode a string with particular HTML entities is the “parseFromString()” methodology. It’s a pre-built methodology of the “DOMParser” object. It helps to parse an XML or HTML string after which create a brand new DOM doc object from it.

Instance

First, create a brand new object of the “DOMParser” utilizing the “new” key phrase:

const parser = new DOMParser();

Name the “parseFromString()” methodology and cross the parameters “encoded string” as a fancy HTML construction, and the “textual content/html”. It tells the tactic to deal with the encoded string as HTML. Use the “textContent” property of the physique factor to get the decoded string:

const decodedString = parser.parseFromString(`<!doctype html><physique>${encodedString}`, ‘textual content/html’).physique.textContent;

Print the decoded string on the console:

console.log(“Decoded String: “ + decodedString);

Output

We have now supplied all of the important directions related to decoding a string with particular HTML entities.

Conclusion

For decoding a string that accommodates particular HTML entities in it, make the most of the HTML factor “textarea” or the

parseFromString()” methodology of the “DOMParser” object. The <textarea> strategy is appropriate for easy situations whereas the parseFromString() methodology is a extra sturdy and safe strategy that may deal with advanced HTML buildings. It’s endorsed to make use of the “parseFromString()” methodology to decode a string containing HTML entities. This submit described the suitable method for decoding strings with particular HTML entities.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments