RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Strip HTML tags with exceptions
Expression
<\/*?(?![^>]*?\b(?:a|img)\b)[^>]*?>
Description
This regex will match all HTML tags except 'a' tags or 'img' tags. You can edit the list of exclusions as you see fit. I use this regex to strip all HTML tags from source data except anchor tags and image tags.
Matches
<script> </html> <anytag>
Non-Matches
<a> <img /> </a>
Author Rating: Not yet rated. Charles Forsyth
Source
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
Comment
 
Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Use replace
Name: Charles
Date: 1/7/2016 1:58:40 PM
Comment:
Use replace. This matches any tag that is not in the list, as described. So replace any match with an empty string to remove tags that are not <a> or <img>. But you can add any tagname you wish with or pipe delimiters. e.g. (a|i|b|strong)


Title: Seems to do the opposite
Name: Neil McLeish
Date: 1/4/2012 5:13:45 AM
Comment:
Hi. I hav etried this and the tags which got stripped from my source were the anchor tags. This regex therefore, seems to do the exact opposite of its decription. Regards, Neil.


Copyright © 2001-2018, RegexAdvice.com | ASP.NET Tutorials