gpt4 book ai didi

c# - 使用正则表达式 c# 去除除 之外的 html 标签

转载 作者:行者123 更新时间:2023-12-02 05:05:39 24 4
gpt4 key购买 nike

我想去掉所有的 html 但保留 <b>使用正则表达式的标签。有没有更好的方法来代替

  1. 替换<b>使用非 html 标签,如 $b$
  2. 使用 <[^>]*> 删除所有 html 标签
  3. 将 $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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com