JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
JavaScript
var whitelist = ["src", "href", "style"];
$( document ).ready(function() {
function foo(contents) {
var temp = document.createElement('div');
var html = $.parseHTML(contents);
temp = $(temp).html(contents);
$(temp).find('*').each(function (j) {
var attributes = this.attributes;
var i = attributes.length;
while( i-- ) {
var attr = attributes[i];
if( $.inArray(attr.name,whitelist) == -1 )
this.removeAttributeNode(attr);
}
});
return $(temp).html();
}
var raw = '<title>Hello World</title><div style="margin:0px;" fadeout"="" class="xyz"><img src="abc.jpg" alt="" /><p style="margin-bottom:10px;">The event is celebrating its 50th anniversary Kö <a href="http://www.germany.travel/" style="margin:0px;">exhibition grounds in Cologne</a>.</p><p style="padding:0px;"></p><p style="color:black;"><strong>A festival for art lovers</strong></p></div>'
alert(foo(raw));
});