Reputation: 6697
I have this string:
var str = '<p>paragraph<a>link</a></p>
<div class="myclass">
<div>something</div>
<div style="mystyle">something</div>
<b><a href="#">link</a></b>
<b><a href="#" name="a name">link</a></b>
<b style="color:red">bold</b>
<img src="../path" alt="something" />
<img src="../path" alt="something" class="myclass" />
</div>';
I want to remove all attributes except href
, src
, alt
. So this is expected result:
/*
<p>paragraph<a>link</a></p>
<div>
<div>something</div>
<div>something</div>
<b><a href="#">link</a></b>
<b><a href="#">link</a></b>
<b>bold</b>
<img src="../path" alt="something">
<img src="../path" alt="something">
</div>
How can I do that?
I can just select them which isn't useful:
/<[a-z]+ .*?(href|src|alt)="[^"]+"/g
Upvotes: 0
Views: 2156
Reputation: 8183
var str = `<p>paragraph<a>link</a></p>
<div class="myclass">
<div>something</div>
<div style="mystyle">something</div>
<b><a href="#">link</a></b>
<b><a href="#" name="a name">link</a></b>
<b style="color:red">bold</b>
<img src="../path" alt="something" />
<img src="../path" alt="something" class="myclass" />
</div>`;
var $div =$("<div>");
$div.html(str);
$div.find("*").each(function() {
var attributes = $.map(this.attributes, function(item) {
return item.name;
});
// now use jQuery to remove the attributes
var element = $(this);
$.each(attributes, function(i, item) {
if (["href", "src", "alt"].indexOf(item) == -1)
element.removeAttr(item);
});
});
console.log($div.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 1
Reputation: 177754
var str = `<p>paragraph<a>link</a></p>
<div class="myclass">
<div>something</div>
<div style="mystyle">something</div>
<b><a href="#">link</a></b>
<b><a href="#" name="a name">link</a></b>
<b style="color:red">bold</b>
<img src="../path" alt="something" />
<img src="../path" alt="something" class="myclass" />
</div>`;
var div = document.createElement("div");
div.innerHTML=str;
div.querySelectorAll("*").forEach(function(el){
for (var i = 0, atts = el.attributes, n = atts.length; i < n; i++){
var att = atts[i].nodeName;
if (["src","alt","href"].indexOf(att) ==-1) el.removeAttribute(att);
}
});
// console.log(div); alert shows it more clearly
alert(div.innerHTML);
PS: Please note that you need backticks to quote a string with embedded newlines
Upvotes: 4