62,268
社区成员
发帖
与我相关
我的任务
分享 string str = @" <div id=""divcontainer"" class=""style"" lang=""zh-CN"">helloworld </div> ";
string sProperty = "id";
str = System.Text.RegularExpressions.Regex.Replace(str, @"<(\w+)[^>]*?(" + sProperty + @"=(['""])?[^\3]*?\3)[^>]*?>", "<$1 $2>");
Response.Write(Server.HtmlEncode(str));
Response.Write("<BR>");
//第二个例子
str = @" <a id=""divcontainer"" class=""style__''''______"" lang=""zh-CN"">helloworld </a> ";
sProperty = "class";
str = System.Text.RegularExpressions.Regex.Replace(str, @"<(\w+)[^>]*?(" + sProperty + @"=(['""])?[^\3]*?\3)[^>]*?>", "<$1 $2>");
Response.Write(Server.HtmlEncode(str));
string re = "(?<=<div[\\s\\S]*)class=([\'\"]?).+\\1\\s*lang=([\'\"]?).+\\2>";
string source = "如果有这样的html. " +
"<div id=\"divcontainer\" class=\"style\" lang=\"zh-CN\">helloworld </div> ";
string result = Regex.Replace(source, re, ">");
Response.Write(result);