Home > DeveloperSection > Forums > Convert special characters to HTML in Javascript
Ankita Pandey
Ankita Pandey

Total Post:183

Posted on    September-03-2013 7:29 AM


 1 Reply(s)
 1177  View(s)
Rate this:

Does any one know how to convert special characters to HTML in Javascript?


'&' (ampersand) becomes '&amp' 
'"' (double quote) becomes '&quot' when ENT_NOQUOTES is not set.
''' (single quote) becomes '&#039' only when ENT_QUOTES is set.
'<' (less than) becomes '&lt'
'>' (greater than) becomes '&gt'

Thanks in advance.

Sumit Kesarwani

Total Post:378

Posted on    September-03-2013 7:53 AM

Hi Ankita,

You need a function that does something like

return mystring.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");

But taking into account your desire for different handling of single/double quotes.

Don't want to miss updates? Please click the below button!

Follow MindStick