开发者

Need to modify my Regex to ignore iframe tags

开发者 https://www.devze.com 2023-03-05 05:36 出处:网络
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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消