Posted By

enchance on 09/06/09


Tagged

html convert entities


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

guido


Convert to HTML entities


 / Published in: JavaScript
 

  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  

You need to login to post a comment.