从页面上读出整个html代码,想把其中"<table id='' class='ve-wrap'><tr><td></td></tr></table>"的标签全部去掉,怎样用正则表达式替换

解决方案 »

  1.   

    <script language="JavaScript" type="text/JavaScript">
    <!--
    str = "<a href=\"xxx.asp\"><div>1234567</div>89</a>";
    alert(str.replace(/<.*?>/g,""));
    //-->
    </script>
    正则就是把<>中间的东西都去掉了
      

  2.   

    <script>
    var s="d <table id='' class='ve-wrap'> <tr> <td> </td> </tr> </table> s";
    s=s.replace(/<(table).*?[<][/]\1>/g,"")
    alert(s);
    </script>
      

  3.   


     <div id=div1>
      <table id="tab1" class='ve-wrap'>
      <tr>
    <td>1</td>
    <td>2</td>
      </tr>
      <tr>
    <td>www</td>
    <td>四</td>
      </tr>
      </table>
      </div>
      <SCRIPT LANGUAGE="JavaScript">
      <!--
    var str = div1.outerHTML;
    var re = /<\/?t.*?>/ig;
    var s = str.replace(re,"");
    alert(s);  //-->
      </SCRIPT>
      

  4.   

    不需要用到正则表达式就能够实现,而且不容易出错:<script type="text/javascript">
    function DelTab(id) {
    var $ = document.getElementById(id);
    $.outerHTML = "";
    }
    </script>
    <div id="div1">
    <table id="tab1" border="1">
    <tr>
    <td>tab1_00</td>
    <td>tab1_01</td>
    </tr>
    <tr>
    <td>tab1_10</td>
    <td>tab1_11</td>
    </tr>
    <tr>
    <td>tab1_20</td>
    <td>tab1_21</td>
    </tr>
    </table>
    <br />
    <table id="tab2" border="1">
    <tr>
    <td>table2_00</td>
    <td>table2_01</td>
    </tr>
    <tr>
    <td>table2_10</td>
    <td>table_11</td>
    </tr>
    <tr>
    <td>table2_20</td>
    <td>table2_21</td>
    </tr>
    </table>
    <br /></div>
    <input type="button" value="删除表格1" onclick="DelTab('tab1')" />&nbsp;
    <input type="button" value="删除表格2" onclick="DelTab('tab2')" /><br />
      

  5.   

    Re:1# 2# 3#,都不严谨而出错了。
    Re:4# 正则最不容易出错。
    CDOE:
    <textarea id="contents" style="width:500px;height:500px">
    <div id="div1">
    <table id="tab1" border="1">
        <tr>
            <td>tab1_00</td>
            <td>tab1_01</td>
        </tr>
        <tr>
            <td>tab1_10</td>
            <td>tab1_11</td>
        </tr>
        <tr>
            <td>tab1_20</td>
            <td>tab1_21</td>
        </tr>
    </table>
    <br />
    <table id="tab2" border="1">
        <tr>
            <td>table2_00</td>
            <td>table2_01</td>
        </tr>
        <tr>
            <td>table2_10</td>
            <td>table_11</td>
        </tr>
        <tr>
            <td>table2_20</td>
            <td>table2_21</td>
        </tr>
    </table><br />
    </div>
    </textarea>
    <script type="text/javascript">
    var str=document.getElementById("contents").value;str=str.replace(/<.*?>/g,"");//1#出错
    str=str.replace(/<(table).*?[<][/]\1>/g,"");//2#出错
    str=str.replace(/<\/?t.*?>/g,"");//3#出错str=str.replace(/<table[\S|\s]*?\/table>/g,"");//请问4#这有什么错?

    alert(str)
    </script>
      

  6.   

    这论坛就是发帖异常了还不能够编辑,只好重发.....
    <script type="text/javascript">
    var str=document.getElementById("contents").value;str=str.replace(/<.*?>/g,"");//1#出错 
    str=str.replace(/<(table).*?[<][/]\1>/g,"");//2#出错 
    str=str.replace(/<\/?t.*?>/g,"");//3#出错 
    str=str.replace(/<table[\S|\s]*?\/table>/g,"");//请问4#这有什么错?alert(str)
    </script>
      

  7.   

    咦?出错?不是要把表格标签都去掉只留内容吗?alert出来没错啊……
    还是我题目理解错了?我是3#的
      

  8.   

    是把标签去掉,留内容?
    我还以为整个表格连内容都去掉呢……
    回6#,你的代码运行出来的结果是:
    ---------------------------
    来自网页的消息
    ---------------------------
     <div id="div1"> <table id="tab1" border="1">            tab1_00        tab1_01                tab1_10        tab1_11                tab1_20        tab1_21     </table> <br /> <table id="tab2" border="1">            table2_00        table2_01                table2_10        table_11                table2_20        table2_21     </table> <br /> </div> 
    ---------------------------
    确定   
    ---------------------------貌似也不对,而且连用了4个正则,还留有其它信息。
    我说的容易出错是指写正则的时候,一不小心就会将两个表格都处理了,要不你看看只将第一个表格标签去掉,第二个表格留着试试,当然,取字符串的时候要将两个表格都取了
      

  9.   

    我是4#,8#<script type="text/javascript">
        function DelTab(id) {
            var $ = document.getElementById(id);
    alert("正则:\n"+$.outerHTML.replace(/<[^>]*>/g,"")+"\n非正则:\n"+$.innerText)
            
    /*
    //用正则表达式得到的结果
    $.outerHTML = $.outerHTML.replace(/<[^>]*>/g,""); //当然,这个结果在网页中显示是不换行的
    */

    /*
    //非正则表达式得到的结果
    $.outerHTML = $.innerText;//同上,在网页中显示结果不换行
    */
        }
    </script>
    这些代码替换4#中的JS代码可以去掉标签而保留内容
      

  10.   

    re 8#,非要我这样写?
    [code]
    <script type="text/javascript">
    var str=document.getElementById("contents").value;str=str.replace(/<.*?>/g,"");//1#出错
    alert(str);
    str=str.replace(/<(table).*?[ <][/]\1>/g,"");//2#出错
    alert(str);
    str=str.replace(/<\/?t.*?>/g,"");//3#出错
    alert(str);str=str.replace(/<table[\S|\s]*?\/table>/g,"");//请问4#这有什么错?
    alert(str)
    </script>
    [/code]
      

  11.   

    re 8#,貌似要我这样写...<script type="text/javascript">
    var str=document.getElementById("contents").value;str=str.replace(/<.*?>/g,"");
    alert(str);//1#出错
    str=str.replace(/<(table).*?[ <][/]\1>/g,"");
    alert(str);//2#出错
    str=str.replace(/<\/?t.*?>/g,"");
    alert(str);//3#出错str=str.replace(/<table[\S|\s]*?\/table>/g,"");
    alert(str);//请问4#这有什么错?
    </script>
      

  12.   

    本来我不想说你的,你代码里面的 str 值一直在改变,
    也就是说你从头到尾一共用了4个正则表达式,你要真的想说明点啥,你应该试试这样:var str=document.getElementById("contents").value; alert(str.replace(/ <.*?>/g,""));//1#出错 
    alert(str.replace(/ <(table).*?[ <][/]\1>/g,"") );//2#出错 
    alert(str.replace(/ <\/?t.*?>/g,"") );//3#出错 
    alert(str.replace(/ <table[\S|\s]*?\/table>/g,"") );//请问4#这有什么错? 
      

  13.   

    难道我就不知道str的值一直在变?吹毛球痴!