簡體   English   中英

在C#中使用正則表達式刪除CSS樣式

[英]remove css style with regex in c#

我想從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>

我該如何修復正則表達式以執行此解決方案

我在常規測試員的幫助下解決了

(width =)(\\ s +“ |”)(\\ d + px“ | \\ s + \\ d + px” | \\ s + \\ d + px \\ s +“ | \\ d + px \\ s +”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM