我有这样一段文本。<p><a>#p#分页#e#<a></p>基本上只这样的文本。其中 #p#分页#e# 是要保留的字符串。 但是两边分别由很多成对的的HTML标记。但不固定式<p>或者<a>也可能是其他。
请问我怎么从一段html代码中。只把#p#分页#e#的标记去掉。但是保留其他正常的HTML呢?
是不是要正则表达式做?我不懂正则 哪位好心的前辈给我写一段完整的代码吧.谢谢了 。。时间比较急切实在来不及临时学了.再次谢谢!

解决方案 »

  1.   

    string.replace(...) 
    具体可以查查帮助
      

  2.   

    是要替换完就剩<p></p> 或</a><a> 么?  
      

  3.   


    string text = "..."; 
    string s=System.Text.RegularExpressions.Regex.Replace(text,@"(?<=#>)([^#]*?)(?=#)","",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    s=.s.Replace("#","");
    Response.Write(s);
      

  4.   

    用document.getElementsById()得到<a>#p#分页#e# <a>这个id 然后用得到对象的.innerText=null来替换掉如果没有id等确定的话就比较麻烦了因为有<a>标签的很多
    不得已可以用循环function hide(){
    var SpanList=document.getElementsByTagName('A');
        for(var i = 1;i <SpanList.length;i++)
        {
            if(SpanList[i].innerText.indexOf('#p#分页#e#')!="-1")//
        {
            SpanList[i].innerText = null;
            break;
        }
        }
    }
      

  5.   

    /*
     * 我有这样一段文本。 
     *  <p> <a>#p#分页#e# <a> </p>
     * 基本上只这样的文本。
     * 其中 #p#分页#e# 是要保留的字符串。 但是两边分别由很多成对的的HTML标记。但不固定式 <p>或者 <a>也可能是其他。
     * 请问我怎么从一段html代码中。只把#p#分页#e#的标记去掉。但是保留其他正常的HTML呢?
     * 是不是要正则表达式做? 
     * 我不懂正则 哪位好心的前辈给我写一段完整的代码吧.谢谢了 。。时间比较急切实在来不及临时学了.
     * 再次谢谢!
     */
    string str = "<p> <a>#p#分页#e# <a> </p> ";
    string temp=Regex.Replace(str, "<[^<>]+>", "").Trim();
    Console.WriteLine(temp);
    /*
    #p#分页#e#
    请按任意键继续. . .
    */
      

  6.   

    string str = "<p> <a>#p#分页#e# <a> </p> ";
    string temp=Regex.Replace(str, "<[^<>]+>", "").Trim();
    Console.WriteLine(temp);
      

  7.   

    <p>你好。测试。和阿斯顿飞 </p>
    <p><span>p#分页#e#</span></p>
    <p>asdfasdfa;dsfjafjkasdfafj;asdfjadsfask;ldf</p>
    <p>asdfkjajdlfk;ajslkdfjladf</p>
    <p>adsfkjasjfklaldfas;df</p>
    <div><p>&nbsp;#p#分页#e#</p></div>要替换成<p>你好。测试。和阿斯顿飞 </p>
    p#分页#e#
    <p>asdfasdfa;dsfjafjkasdfafj;asdfjadsfask;ldf</p>
    <p>asdfkjajdlfk;ajslkdfjladf</p>
    <p>adsfkjasjfklaldfas;df</p>
    &nbsp;#p#分页#e#
    总之就似乎说
    所有属于
    "#p#分页#e#"
    这个的html标记都要去掉。但是其他的不去掉
      

  8.   

    而且。包围住"p#分页#e#"的标记不是固定的。不一定是p或者span,也有可能是其他标记的。
      

  9.   

    到底要去掉什么 ? 是 #p#分页#e# 还是 外围的 html ?p 和 e 是通配符?
      

  10.   

    试试这个:
    string text="<p>你好。测试。和阿斯顿飞 </p><p><span>p#分页#e#</span></p><p>asdfasdfa;dsfjafjkasdfafj;asdfjadsfask;ldf</p><p>asdfkjajdlfk;ajslkdfjladf</p><p>adsfkjasjfklaldfas;df</p><div><p>&nbsp;#p#分页#e#</p></div>";
    text = System.Text.RegularExpressions.Regex.Replace(text, @"(\<(?!/)[^>]+?\>)+(?<text>[^<>]*?p\#分页\#e\#)(\</[^>]+?\>)+", "${text}");
      

  11.   

    用正则替换掉吧
    string str= "<html><body>fdf</body></html>";
    Regex reg = new Regex(str, "<[^< >]*?>", "");
      

  12.   

    楼主需求还是不太明确
    如果是下面这样的呢?
    <div>
    <p>你好。测试。和阿斯顿飞 </p>
    <p><span>p#分页#e#</span></p>
    </div>那最外层的div要保留么?
      

  13.   

    string str = "<head runat='server'><title>Untitled Page</title></head><body><form id='form1' runat='server'><div>";
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("<[^<>]*>");
    str = reg.Replace(str, "");
      

  14.   

    http://blog.csdn.net/hy_lihuan/archive/2007/12/25/1967212.aspx