I have a bunch of divs throughout a page, and I don't control where the content comes from. I want to be sure those divs contain no XHTML, except for <p> tags.
A simple jQuery solution should do the trick.
With this code, any element with class 'sanitize' will be taken care of:
$('.sanitize').each(function() { $(this).html( $(this).html().replace(/\<\/?[^p ]{1,2}[^>]*>/ig,'') ) });
If you needed to retain some tag other than <p>, you could simply modify this portion of the RegEx:
[^p ]
...where 'p' is the name of the tag.
Thanks to Jon for his RegEx mastery, and Pete for his jQuery shenanigans.
3 Comments
Would multiple tag exclusion be like:
[^p|^a|^table]
?
Jon says: "This doesn't eliminate the <param> tags."
@Pete Karl II, yes, I do believe so.
Post new comment