[英]How to remove/change font formatting for string?
如何從數據庫中的字符串中刪除字體類型並使用ckeditor保存? 例如:
<div style="font-family: Tahoma; color: Red;">
Foo FOooooo
</div>
我想刪除它或將其更改為Verdana。
我知道我可以使用替換,但字體名稱可以不同,我知道我可以使用substring方法。 但有沒有簡單的方法來刪除它?
有兩種方法,簡單的方法,只需使用“簡單”正則表達式刪除完整的div樣式
private static Regex oClearHtmlScript = new Regex(@"<(.|\n)*?>", RegexOptions.Compiled);
public static string StripHTML(string sHtmlKeimeo)
{
if (string.IsNullOrEmpty(sHtmlKeimeo))
return string.Empty;
return oClearHtmlScript.Replace(sHtmlKeimeo, string.Empty);
}
並且很難,使用Html Agility Pack(或任何其他類似的)來解析html並直接更改屬性。
通過這個regex
嘗試這種簡單的方法:
捕獲font-family
和color
樣式:
<div\s+style=\".*?font-family:(?<fontName>\s*[^;\"]*)?.*?color:(?<color>\s*[^;\"]*)?
和您的替換代碼:
String inputStr = "<div style=\"font-family: Tahoma; color: Red;\">";
foreach(Match m in Regex.Matches(inputStr, "<div\\s+style=\\\".*?font-family:(?<fontName>\\s*[^;\\\"]*)?.*?color:(?<color>\\s*[^;\\\"]*)?"))
{
inputStr = inputStr.Replace(m.Groups["fontName"].Value, "Vernada").Replace(m.Groups["color"].Value, "Blue");
}
說明:
(?<name> subexpression)
將匹配的子表達式捕獲到命名組中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.