本帖最后由 yaochutao 于 2011-03-07 14:25:58 编辑

解决方案 »

  1.   

    何必用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\">");
    }
      

  2.   

                
    //投机取巧型
    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"/>
    */
      

  3.   


    var inputs = document.getElementsByTagName("input");
    for(var i=0;i<inputs.length;i++){
    inputs[i].value="hello";
    }
      

  4.   

                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"/>
    */
      

  5.   


    //最终版本,经过客指导了个类似的题目
                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"/>
    */