我想删除所有 html 但保留 <b>
使用正则表达式的标签。有没有更好的方法来代替
- 替换
<b>
带有非 html 标签,如 $b$ - 使用
<[^>]*>
删除所有 html 标签 - 将 $b$ 替换为
<b>
最佳答案
下面是一种只允许打开和关闭的方法 b
标签。任何其他标签都将被删除。
var teststring = "Test <b>test</b> lorem <i>ipsum</i>";
var pattern = @"(?!</?b>)<.*?>"; // assuming open and closing tags are retained
Console.WriteLine(Regex.Replace
(teststring,
pattern,
String.Empty,
RegexOptions.Multiline));
输出:Test <b>test</b> lorem ipsum
关于c# - 使用正则表达式 C# 去除除 <b> 之外的 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16311520/