-1

您好,我知道用正则表达式解析 HTML 效率不高。但我需要使用正则表达式,我别无选择。

HTML

    <div class="test">
        <h2>what</h2>


        <table cellpadding="0" cellspacing="0">
            <tbody>

        <tr>
            <th>Example                            </th>
            <td> ui                         </td>
        </tr>

        <tr>
            <th>Sample                            </th>
            <td>123                     </td>
        </tr>

            </tbody>
        </table>
    </div>

我尝试使用(?s)<div class="test">.*<td>(.*?)</td>.*</div>它仅提取最后一个值来做到这一点,任何人都可以告诉我有什么问题吗?

4

2 回答 2

0

为什么只使用正则表达式,一些jquery怎么样?

$("div.test > td").each(function() { 
    var $this = $(this);
    alert( $this.text() ) 
});
于 2013-04-30T10:08:51.817 回答
0

*操作员尽可能多地阅读,所以第一个也.*吞下大部分文本。

尝试使用.*?. 问号减少了这种行为,让我们*只需要尽可能多的,而不是尽可能多的。

否则,请更具体地说明您真正想要哪些部分,哪些不是。

于 2013-04-30T10:09:03.627 回答