string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";获取hello.html和hello2.html

解决方案 »

  1.   


    var str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
    var reg = /href="([^"]+?html)"/g;
    var urls = [];
    while(reg.exec(str)){
    urls.push(RegExp.$1);
    }
    alert(urls)
      

  2.   

    哦,补充下,我的失误,不好意思
    那个1=》 1.html
      

  3.   

    哦 你会C#吗,刚才发错板块了
    <a href=\"(?<url>.*?)\" target=\"_blank\">.*?</a> 这个是我写的正则 哪儿有问题
    你的这个在js里OK
      

  4.   

    c#的更简单了
    不过不懂你的1.html和其他的有什么区别吗
      

  5.   

    string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1.html\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
                MatchCollection mc = Regex.Matches(str,@"(?<=href="")[^\""]+");
                foreach (Match m in mc)
                {
                    Console.WriteLine(m);
                }
      

  6.   

    嗯 那好办
     有区别,就是说我只获取  target=\"_blank\" 这个链接
      

  7.   

    string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1.html\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
                MatchCollection mc = Regex.Matches(str, @"(?i)(?<=href="")[^\""]+(?=[^>]*?target=""_blank"")");
                foreach (Match m in mc)
                {
                    Console.WriteLine(m);
                }
      

  8.   

    稍微严谨点。
    string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1.html\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
                MatchCollection mc = Regex.Matches(str, @"(?i)(?=[^<>]*?target=""_blank"")(?<=href="")[^\""]+");
                foreach (Match m in mc)
                {
                    Console.WriteLine(m);
                }
      

  9.   

    try...            string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
                Regex reg = new Regex(@"(?i)<a(?=[^>]*?target=""_blank"")[^>]*?href=""([^""]*)""");  
                MatchCollection mc = reg.Matches(str);
                foreach (Match m in mc)
                {
                    richTextBox2.Text += m.Groups[1].Value + "\n";
                }
      

  10.   

    应该是这个吧
    MatchCollection mc = Regex.Matches(str, @"(?i)(?<=<a\s+href="")[^""]+(?=""\s+target=""_blank"")");
    不然下面情况会取错的
    string str = "<a href=\"hello.html\" target=\"_blank\">href=\"coco.html\" target=\"_blank\"</a><a href=\"1.html\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
      

  11.   

    谢谢大家的答案,正确的
    我的这个怎么修改:<a href=\"(?<url>.*?)\" target=\"_blank\">.*?</a> 
    我想使用这种方式,不用索引的
    另外关键点解释下就最好了
      

  12.   

    MatchCollection mc = Regex.Matches(str,@"(?i)(?<=<a\s[^>]*href[^=]*=[^=\x22']*(['\x22]?))((?!\1).)+\.html");
    foreach(Match m in mc)
    {
        m.Value;就是你要的结果。
    }
      

  13.   

    嗯  我想 Groups["url"] 这种获取方式,请问怎么修改
      

  14.   

    在看清楚别人问题的时候再回答,你回答我表示感谢,你骂人我没有必要跟你争论我说的是Groups["url"]这种方式,不是索引的形式,难道你没有看懂?
      

  15.   

    过客的修改一点。加个名字
    string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
    Regex reg = new Regex(@"(?i)<a(?=[^>]*?target=""_blank"")[^>]*?href=""(?<url>[^""]*)""");  
    MatchCollection mc = reg.Matches(str);
    foreach (Match m in mc)
    {
    richTextBox2.Text += m.Groups["url"].Value + "\n";
    }