views:

48

answers:

2

I'm looking for a regular expression that can extract the href from this:

<a href="/tr/blog.php?post=3593&user=930">

There are hundreds of links on the page so I need to extract only those that contain

/tr/blog.php

So in the end I should be left with a list of links that start in /tr/blog

Thanks for any help. It's really puzzling me.

This is the RegEx I am currently using, but it matches all.

/href\s*=\s*\"*[^\">]*/ig;
+2  A: 

You could try something like href=\"(/tr/blog.php[^"]*)\" (will capture to Group 1), but in general you should not use regex to parse HTML.

VeeArr
A: 
<body> <a href="/tr/blog.php?lol">fslk</a> 

<script>

    var anchors = document.getElementsByTagName('a'), captured = [];

    for ( var i = 0, l = anchors.length, href, r = /tr\/blog\.php/; i<l; ++i ) {
         href = this.href;
         if ( r.test( href ) ) {
             captured.push( this )
         }
    }

    // do what u want with captured links
    for ( var l = captured.length; l--; ) {
        alert( captured[l].href )
    }

</script>

</body>
meder