Removing empty HTML tags from a string with Javascript

Removing empty HTML tags from a string is a rather common task when you let the user modify the HTML with a WYSIWYG editor, giving the user so much freedom is normally because he or she is the administrator, normally you don't want to just let them input HTML, as the potential for XSS and other kind of bad intentioned inputs are possible.

Nevertheless, if you just need to clean the string, this is a nice Javascript function which clean all empty HTML tags.

 String.prototype.htmlTrim = function () {  
   return this.replace(/<(\w+)>\s*\n*\t*(&nbsp;)*\s*\n*\t*<\/\1>/, '');  
 };  

The usage is just

 var cleanString = "<p></p><p>My String</p>".htmlTrim();  

That will return "<p>My String</p>". Here's an example of the Regex.