Convert to HTML entities


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. String.prototype.htmlEntities = function () {
  2. return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
  3. };
  4.  
  5.  
  6. //Usage 1:
  7. var tmp = '<html><head></head>';
  8. var safe= tmp.htmlEntities(); // Returns
  9. "&lt;html&gt;&lt;head&gt;&lt;/head&gt;"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.