width=str.replace(/(.*)(width=)['|"]?(\w*)['|"]?(\s)(.*)/,"$3");

解决方案 »

  1.   

    width=100 class='c' style='border:1' title="t"我是想取得所有属性:
    width
    class
    style
    title
      

  2.   

    写到一起正则太长,分开写。
    width=str.replace(/(.*)(width=)['|"]?(\w*)['|"]?(\s)(.*)/,"$3");
    class=str.replace(/(.*)(class=)['|"]?(\w*)['|"]?(\s)(.*)/,"$3");
    style=str.replace(/(.*)(style=)['|"]?(\w*)['|"]?(\s)(.*)/,"$3");
    title=str.replace(/(.*)(title=)['|"]?(\w*)['|"]?(\s)(.*)/,"$3");
      

  3.   

    <SCRIPT>
    function ShowAttribs(oElem)
    {
        txtAttribs.innerHTML = '';    // Retrieve the collection of attributes for the specified object.
        var oAttribs = oElem.attributes;    // Iterate through the collection.
        for (var i = 0; i < oAttribs.length; i++)
        {
            var oAttrib = oAttribs[i];        // Print the name and value of the attribute. 
            // Additionally print whether or not the attribute was specified
            // in HTML or script.
            txtAttribs.innerHTML += oAttrib.nodeName + '=' + 
                oAttrib.nodeValue + ' (' + oAttrib.specified + ')<BR>'; 
        }
    }
    </SCRIPT>
      

  4.   

    $s = <<< TEXT
    <table width=100 class='c' style='border:1' title="t" >
    TEXT;
    preg_match_all("/(\w+)=([\"']?)(.+)\\2[\s>]/sU", $s, $r);print_r($r);
      

  5.   

    针对下面的这种情况,能否改成能区别为不同递归层次的?<table width=100 class='c' style='border:1' title="t" >
    <tr><td>
    <table width=15 class='c22' style='border:1' title="标题" >
    <tr><td>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
      

  6.   

    <?php
    $str = <<<EOT
    <table width=100 class='c' style='border:1' title="t" >
    EOT;
    preg_match_all('|([a-z]+)=([\']?)(.+)\2[\s>]|iU', $str, $arr);
    print_r($arr[1]);
    ?>output:Array
    (
        [0] => width
        [1] => class
        [2] => style
        [3] => title
    )
      

  7.   

    能不能得到这样的结果:
        [0] => Array
            (
                [0] => width=100 
                [1] => class='c' 
                [2] => style='border:1' 
                [3] => title="t" 
            ),
        [1] => Array
            (
                [4] => width=15 
                [5] => class='c22' 
                [6] => style='border:1' 
                [7] => title="标题" 
            )
      

  8.   

    <?php
    $str = <<<EOT
    <table width=100 class='c' style='border:1' title="t" >
    EOT;
    preg_match_all('|(([a-z]+)=([\']?)(.+)\3)[\s>]|iU', $str, $arr);
    print_r($arr[1]);
    ?>output:Array
    (
        [0] => width=100
        [1] => class='c'
        [2] => style='border:1'
        [3] => title="t"
    )
      

  9.   

    谢谢,是针对本身就嵌套的内容:
    $str = <<<EOT
    <table width=100 class='c' style='border:1' title="t" >
    <tr><td>
    <table width=15 class='c22' style='border:1' title="标题" >
    <tr><td>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    EOT;
    我想得到的结果是:
        [0] => Array
            (
                [0] => width=100 
                [1] => class='c' 
                [2] => style='border:1' 
                [3] => title="t" 
            ),
        [1] => Array
            (
                [4] => width=15 
                [5] => class='c22' 
                [6] => style='border:1' 
                [7] => title="标题" 
            )目前得到的结果是:
        [1] => Array
            (
                [0] => width=100
                [1] => class='c'
                [2] => style='border:1'
                [3] => title="t"
                [4] => width=15
                [5] => class='c22'
                [6] => style='border:1'
                [7] => title="标题"
            )
      

  10.   

    没有时间了,这个办法有点笨<?php
    $str = <<<EOT
    <table width=100 class='c' style='border:1' title="t" >
    <tr><td>
    <table width=15 class='c22' style='border:1' title="标题" >
    <tr><td>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    EOT;
    preg_match_all('|<table[^>]+>|i', $str, $s);foreach ($s[0] as $v)
    {
    preg_match_all('|(([a-z]+)=([\']?)(.+)\3)[\s>]|iU', $v, $arr);
    $r[] = $arr[1];
    }
    print_r($r);
    ?>output:
    Array
    (
        [0] => Array
            (
                [0] => width=100
                [1] => class='c'
                [2] => style='border:1'
                [3] => title="t"
            )    [1] => Array
            (
                [0] => width=15
                [1] => class='c22'
                [2] => style='border:1'
                [3] => title="标题"
            ))
      

  11.   

    错了,应该是
    (?<=\s)([^\s]+?)
      

  12.   

    (?<=\s)([^\s]+?)
    得到结果是:
    Array
    (
        [0] => Array
            (
                [0] => width=100
                [1] => class='c'
                [2] => style='border:1'
                [3] => title="t"
                [4] => >
                [5] => <tr><td>
                [6] => <table
                [7] => width=15
                [8] => class='c22'
                [9] => style='border:1'
                [10] => title="标题"
                [11] => >
                [12] => <tr><td>
                [13] => </td>
                [14] => </tr>
                [15] => </table>
                [16] => </td>
                [17] => </tr>
                [18] => </table>
            )    [1] => Array
            (
                [0] => width=100
                [1] => class='c'
                [2] => style='border:1'
                [3] => title="t"
                [4] => >
                [5] => <tr><td>
                [6] => <table
                [7] => width=15
                [8] => class='c22'
                [9] => style='border:1'
                [10] => title="标题"
                [11] => >
                [12] => <tr><td>
                [13] => </td>
                [14] => </tr>
                [15] => </table>
                [16] => </td>
                [17] => </tr>
                [18] => </table>
            ))