======== 急用,高手请进,求C#一正则 本帖最后由 yaochutao 于 2011-03-07 14:25:58 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 何必用c#?直接用jsvar inputs = document.getElementsByTagName("INPUT");for(var i=0;i<inputs.length;i++){inputs[i].value="hello";}string strHtml ="<input....";Regex r = null;Match m = null;r = new Regex( @"value\s*=\s*""[^""]*""", RegexOptions.IgnoreCase);//先去掉所有value=for (m = r.Match(strHtml); m.Success; m = m.NextMatch()){ strHtml = strHtml.Replace(m.Groups[0].ToString().Trim(), "");}r = new Regex( @"<input([^>]*)>", RegexOptions.IgnoreCase);//先去掉所有value=for (m = r.Match(strHtml); m.Success; m = m.NextMatch()){ strHtml = strHtml.Replace(m.Groups[0].ToString().Trim(), "<input$1 value=\"hello\">");} //投机取巧型string str = "<input type=\"text\" value=\"abc\"/>" + "<input type=\"text\" />" + "<input type=\"text\" value=\"bcc\" class=\"common\"/>"; string result = Regex.Replace(str, @"<input[^>]*?type=(['""\s]?)text\1[^>]*?>", "<input type=\"text\" value=\"hello\"/>"); Response.Write(result);/*<input type="text" value="hello"/><input type="text" value="hello"/><input type="text" value="hello"/>*/ var inputs = document.getElementsByTagName("input");for(var i=0;i<inputs.length;i++){inputs[i].value="hello";} string str="<input type=\"text\" value=\"abc\"/>" +"<input type=\"text\" />" +"<input type=\"text\" value=\"bcc\" class=\"common\"/>"; string result = Regex.Replace(str, @"(?is)(?<=<input[^>]*)(value=['""\s]([^'""\s]+)['""\s])", delegate(Match m) { if (m.Groups[2].Value == "hello") return m.Value; else return "value=\"hello\""; }); result = Regex.Replace(result, @"(?is)(<input(?![^>]*?value=['""\s][^'""\s]*?['""\s]))", "$1 value=\"hello\""); Response.Write(result + "<br/>");/*<input type="text" value="hello"/><input value="hello" type="text" /><input type="text" value="hello" class="common"/>*/ //最终版本,经过客指导了个类似的题目 string str = "<input type=\"text\" value=\"abc\"/>" + "<input type=\"text\" />" + "<input type=\"text\" value=\"bcc\" class=\"common\"/>"; string result = Regex.Replace(str, @"(?is)(?<=<input(?:(?!value)[^/])*)(?:value=(['""\s]?)([^'""\s]*)\1(?=[^>]*/>)|(?=/>))", delegate(Match m) { if (m.Groups[2].Value == "hello") return m.Value; else return "value=\"hello\" "; }); Response.Write(result + "<br/>");/*<input type="text" value="hello" /><input type="text" value="hello" /><input type="text" value="hello" class="common"/>*/ 当单引号跟双引号很多时 关于页面刷新的问题 .net 中页面生面周期 是什么概念 提取字段内容循环插入如何实现? .net 连接 access 数据库,怎连接? 关于连接数据库 一个excel的输出问题?请指点. 自定义HttpHandler在iis8.0中不能访问 有两个WEB开发中的问题?各位帮忙解决. 是不是安装了,vs.net 其他的都可以不用装了?就可以运行.net 的程序了? 菜鸟问题 Gridview CSS样式???
直接用jsvar inputs = document.getElementsByTagName("INPUT");
for(var i=0;i<inputs.length;i++){
inputs[i].value="hello";
}string strHtml ="<input....";
Regex r = null;
Match m = null;
r = new Regex( @"value\s*=\s*""[^""]*""", RegexOptions.IgnoreCase);//先去掉所有value=
for (m = r.Match(strHtml); m.Success; m = m.NextMatch())
{
strHtml = strHtml.Replace(m.Groups[0].ToString().Trim(), "");
}
r = new Regex( @"<input([^>]*)>", RegexOptions.IgnoreCase);//先去掉所有value=
for (m = r.Match(strHtml); m.Success; m = m.NextMatch())
{
strHtml = strHtml.Replace(m.Groups[0].ToString().Trim(), "<input$1 value=\"hello\">");
}
//投机取巧型
string str = "<input type=\"text\" value=\"abc\"/>"
+ "<input type=\"text\" />"
+ "<input type=\"text\" value=\"bcc\" class=\"common\"/>";
string result = Regex.Replace(str, @"<input[^>]*?type=(['""\s]?)text\1[^>]*?>", "<input type=\"text\" value=\"hello\"/>");
Response.Write(result);
/*
<input type="text" value="hello"/><input type="text" value="hello"/><input type="text" value="hello"/>
*/
var inputs = document.getElementsByTagName("input");
for(var i=0;i<inputs.length;i++){
inputs[i].value="hello";
}
+"<input type=\"text\" />"
+"<input type=\"text\" value=\"bcc\" class=\"common\"/>";
string result = Regex.Replace(str, @"(?is)(?<=<input[^>]*)(value=['""\s]([^'""\s]+)['""\s])", delegate(Match m)
{
if (m.Groups[2].Value == "hello")
return m.Value;
else
return "value=\"hello\"";
});
result = Regex.Replace(result, @"(?is)(<input(?![^>]*?value=['""\s][^'""\s]*?['""\s]))", "$1 value=\"hello\"");
Response.Write(result + "<br/>");
/*
<input type="text" value="hello"/>
<input value="hello" type="text" />
<input type="text" value="hello" class="common"/>
*/
//最终版本,经过客指导了个类似的题目
string str = "<input type=\"text\" value=\"abc\"/>"
+ "<input type=\"text\" />"
+ "<input type=\"text\" value=\"bcc\" class=\"common\"/>";
string result = Regex.Replace(str, @"(?is)(?<=<input(?:(?!value)[^/])*)(?:value=(['""\s]?)([^'""\s]*)\1(?=[^>]*/>)|(?=/>))", delegate(Match m)
{
if (m.Groups[2].Value == "hello")
return m.Value;
else
return "value=\"hello\" ";
});
Response.Write(result + "<br/>");
/*
<input type="text" value="hello" />
<input type="text" value="hello" />
<input type="text" value="hello" class="common"/>
*/