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



我想编辑html标签的宽度属性和c#中字符串的内联样式例如,将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>

我如何修复regex来完成此解决方案

i在常规测试人员的帮助下解决

(width=)(\s+"|")(\d+px"|\s+\d+px"|\s+[d+px\s+"|\d+px[s+")

相关内容

  • 没有找到相关文章