【发布时间】:2021-08-26 14:49:03
【问题描述】:
我想用另一个单词替换 html 字符串中的单词,但它只能替换确切的单词,而不是它是单词部分拼写的一部分。我遇到的问题是 html 打开或关闭标签或其他 html 元素正在影响正则表达式中匹配的单词,或者它正在替换部分单词。
PostTxt = “<div>The <b>cat</b> sat on the mat, what a catastrophe.
The <span>cat</span> is not allowed on the mat. This makes things complicated; the cat  must go!
</div><p>cat cat cat</p>”;
string pattern = "cat";
//replacement string to use
string replacement = "******";
//Replace words
PostTxt = Regex.Replace(PostTxt, pattern, replacement, RegexOptions.IgnoreCase);
}
我希望它返回。
<div>The <b>***</b> sat on the mat, what a catastrophe. The <span>***</span> is not allowed on the mat. This makes things complicated; the ***&nbsp must go! </div><p>*** *** ***</p>
我们将不胜感激任何建议和帮助。
【问题讨论】:
-
看看 Html Agility Pack html-agility-pack.net
-
你的例子没有编译
-
感谢 Mark PM 关于使用 html-agility-pack.net 解析器的建议。我已经实现了它,似乎可以满足我的需求。
标签: c# html regex string replace