How can I escape HTML special chars in JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

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, ">")
}
raja
Published on 27-Feb-2018 11:17:20
Advertisements