var str = '<a id="1">1</a><span id="2">2</span><a id="3">3</span>';
var re = //gi;
console.log( str.match(re) ) // ["1", "3"]上面re怎么写能得到["1", "3"],

一个正则能取到标签为a的id值吗?

解决方案 »

  1.   

    这样JS下直接string.match,不行吧
    var str = '<a id="1">1</a><span id="2">2</span><a id="3">3</span>';
    var re = /<a.+?id="(\w+)">/ig,res=[];
    str.replace(re ,function(s,s1){ res.push(s1)});
    console.info( res) 
      

  2.   

    <script>var str = '<a id="1">1</a><span id="2">2</span><a id="3">3</span>';
    var re = /<a.+?id="(\w+)">/gim,matches=[];
    var resultList=[];
    while(matches=re.exec(str)){
    resultList.push(matches[1]);
    }alert(resultList);
    </script>
      

  3.   

    你想要哪般?直接一个正则就直接搞定?好像不行,还是replace吧
      

  4.   

    <a[^>]+id\s?=("|'{0,1})\s?(\S)\s?\1>
      

  5.   

    (?is)<a(?!\S)[^>]*?\sid=(['"]?)(\w+)\1