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:
Charles Forsyth
|
Source |
|
Your Rating |
|
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.