gpt4 book ai didi

c# - 在 C# 中使用正则表达式删除 css 样式

转载 作者:行者123 更新时间:2023-11-28 09:11:48 25 4
gpt4 key购买 nike

我想在 c# 中编辑来自 html 标签的宽度属性和来自字符串的内联样式例如将 width="54px"(或 style="width:54px")转换为 width="54px"或(style="width:100%")在我的字符串中可能有这样的宽度:width='54px'(不带双引号)像这样:

<div width= " 25px " ></div>
<div Width= " 63453px " ></div>
<div width='15px' ></div>
<div width= ' 15px' ></div>
<div Width= ' 15px' ></div>
<div width="1 " ></div>
<div Width= " 45"></div>
<div width=" 5454px "></div>
<div width="54px"></div>
<div width="54px" style="width:54px"> </div>

我尝试用这种方法来做到这一点:

  public string _ConvertTagWidth2(string content)
{
string patern1 = "width=\".\\w*\"";
string patern2 = "width='.\\w*'";

string replace1 = "width=\"100%\"";
string replace2 = "width:'100%'";

string output = Regex.Replace(content, patern1, replace1, RegexOptions.IgnoreCase);
output = Regex.Replace(output, patern2, replace2, RegexOptions.IgnoreCase);

return output;
}

但是结果是这样的:

<div width= " 25px " ></div>
<div Width= " 63453px " ></div>

<div width:'100%' ></div>
<div width= ' 15px' ></div>
<div Width= ' 15px' ></div>

<div width="1 " ></div>
<div Width= " 45"></div>

<div width=" 5454px "></div>
<div width="100%"></div>
<div width="100%" style="width:54px"> </div>

我怎样才能修复正则表达式来做这个解决方案

最佳答案

我在regular tester的帮助下解决了

(宽度=)(\s+"|")(\d+px"|\s+\d+px"|\s+\d+px\s+"|\d+px\s+")

关于c# - 在 C# 中使用正则表达式删除 css 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26466488/

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