Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR HTML

how to escape in html

Escaping HTML characters in a string means replacing the:
1.less than symbol (<) with &lt;
2.greater than symbol (>) with &gt;
3.double quotes (") with &quot;
4.single quote (') with &#39;
5.ampersand (&) with &amp;
 ------------------------------
function escape(htmlStr) {
   return htmlStr.replace(/&/g, "&amp;")
         .replace(/</g, "&lt;")
         .replace(/>/g, "&gt;")
         .replace(/"/g, "&quot;")
         .replace(/'/g, "&#39;");        

}

console.log(escape("<script>alert('hi')</script>"));
 
PREVIOUS NEXT
Tagged: #escape #html
ADD COMMENT
Topic
Name
8+9 =