我有一个简单文字的html字符串:
<div><object height="315" width="560"></object><div><object height="315" width="560"></object></div></div>
如何删除<object>
标记及其中的任何内容?所以我想用空字符串替换它,这意味着从<object>
到</object>
的所有内容都应删除?
答案 0 :(得分:1)
你在这里:
var yourString = @"<div><object height=""315"" width=""560""></object><div><object height=""315"" width=""560""></object></div></div>";
yourString = Regex.Replace(yourString, @"<object.+?\/object>", String.Empty);
答案 1 :(得分:1)
如果您需要解析或修改HTML,我建议使用真实的Html-Parser,如HtmlAgilityPack
:
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
foreach (var node in doc.DocumentNode.SelectNodes("//object"))
node.Remove();
// if you need it as string:
var writer = new StringWriter();
doc.Save(writer);
html = writer.ToString();
结果是(也根据需要删除了嵌套的div):
<div><div></div></div>