tags:

views:

49

answers:

3

Currently, I have this line:

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

But, I would like something along the lines of an "if clause" to say,

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

I don't suppose anyone has any ideas?

[EDIT]: I think I may have to do a "FOR EACH" loop... I think.... [/EDIT]

^.^

+1  A: 

replace can take a function as the second parameter

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

I didn't use the hex encoded chars because it's easier to read this way.

RoToRa
This is the right IF statement!! But I would like to keep the links exactly as they are. This seems to replace the link with something else :(
Neurofluxation
I voted you up as well, for the effort put in ;)
Neurofluxation
Could it be that your tags are uppercase. I forgot to consider that. Edited.
RoToRa
+2  A: 
this.html(this.html().replace(/<\/?([b-z]+)[^>]*>/gi, function(match, tag) { 
  return (tag === "a") ? match : ""; 
})); 

If you are looking at leaving the "a" tags in place, change the regular expression from [a-z] to [b-z]

Ardman
Perfect, cheers very much :)
Neurofluxation
That will keep all tags **containing** an "a", like `span`. Apart from that it's exactly the same as mine, which supposedly doesn't work...
RoToRa
Your code definitely removed all my <a>'s im afraid.
Neurofluxation
A: 

jQuery has ":not" pseudo-selector.

$(':not(a)', this).remove()
migajek