Need to modify my Regex to ignore iframe tags
Hi I have a free text editor for blog posts on my website with C# as code behind. I use the following regular expression to strip all HTML tags out of the post 开发者_如何学编程for security:
Regex.Replace(value, @"<(.|\n)*?>", string.Empty);
However I now have a requirement to allow the embedding of youtube videos, which use an iframe, for example:
<iframe width="425" height="349" src="http://www.youtube.com/embed/ttBhGiuMUmU" frameborder="0" allowfullscreen></iframe>
Could someone help me modify this regex to allow for this?
Try this one:
<(?!/?iframe)(.|\n)*?>
The (?!/?iframe)
is a negative lookahead, that checks that the <
is not followed by an iframe
or /iframe
.
I tested online here on regexr
精彩评论