问题描述
我有一个xml,其中也包含一些html标记.插入标签时,由于它是一个自闭合标签,因此会中断页面.像这样:
I have an xml which contains some html tags also. When a tag comes in, it breaks the page because it's a self closing tag. Something like:
<iframe width="420" height="315" src="//www.youtube.com/embed/6krfYKxJFqA" frameborder="0" />
我要替换它并将其转换为:
I want to replace this and convert it to:
任何人都可以提供带有正则表达式的c#代码来执行此操作.我尝试这样做:
Can anyone provide a c# code with regex to do this. I tried doing:
tmp = tmp.Replace("(<iframe[^>]*)(\\s*/>)", "$1></iframe>");
和
tmp = new Regex(@"(<iframe[^>]*)(\\s*/>)").Replace(tmp, "$1></iframe>");
tmp是包含大量代码和此iframe标签作为字符串的xml.
tmp is the xml containing lot of code + this iframe tag as string.
,但没有结果.
推荐答案
在第二个正则表达式中,您不需要使用双反斜杠作为.
In the second regex, you don't need the double backslash as you are using @
.
此外,(< iframe [^>] *)
也匹配最后一个/
,请使用非贪婪的?
运算符:(< iframe [^>] *?)(\ s */>)
Also, (<iframe[^>]*)
also matches the last /
, use the non-greedy ?
operator: (<iframe[^>]*?)(\s*/>)
这篇关于正则表达式替换C#中的自闭html标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!