Computer >> Computer tutorials >  >> Programming >> Javascript

How can I escape HTML special chars in JavaScript?


To escape HTMl special characters in JavaScrip, use the replace() method. The following are our special characters −

&
<
> 

Try the following code snippet to achieve this −

function display(unsafe) {
   return unsafe
      .replace(/&/g, "&")
      .replace(/</g, "<")
      .replace(/>/g, ">")
}