JQuery remove all HTML tags EXCEPT Anchors

I currently have this line:

this.html(this.html().replace(/\x3C\x2F?[^\x3E]+\x3E/gi, ''));

But I would like something like "if" lines to say:

IF (this.tag = "<a") {
  do nothing
} ELSE {
  remove tag
}

I don’t think anyone has any ideas?

[EDIT]: I think I might need a “FOR EVERYONE” loop ... I think .... [/ EDIT]

^. ^

+3
source share
3 answers
this.html(this.html().replace(/<\/?([b-z]+)[^>]*>/gi, function(match, tag) { 
  return (tag === "a") ? match : ""; 
})); 

If you want to leave the tags "a", change the regular expression from [a-z]to[b-z]

+2
source

jQuery has a pseudo-selector ": not".

$(':not(a)', this).remove()
+4
source

replace

this.html(this.html().replace(/<\/?([a-z]+)[^>]*>/gi, function(match, tag) {
  return (tag.toLowerCase() === "a") ? match : "";
}));

, .

+2

All Articles