regular expressions in java

P

passion_to_be_free

I am writing a javascript that will make an http request, sort through
the html for any links on the page, and then store them for future
processing.

To test things, I pasted html source code into my text editor, ran a
GREP search using the following regular expression:

(<a [^>]*href\s*=\s*")([^"]+)("[^>]*>)

And the appropriate links were highlighted correctly. I then
implemented this regular expression into my javascript. However, the
javascript only matched one out of 10 links on the page I was
searching. Can anyone

(a.) tell me what's wrong with my regular expression
(b.) suggest a better one?

Here's my javascript code. I've even tried it two ways (as you'll see
by the commented code):

// make the page request
var xmlhttp;

if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET",querystring,false);
xmlhttp.send(null);

//just to verify I'm getting something back:
//document.write(xmlhttp.responseText);
}

var htmltext = xmlhttp.responseText;

//Method 1:
//create a pattern matcher and execute it
var reg = new RegExp("");
reg.compile('(<a class=l [^>]*href\s*=\s*")([^"]+)("[^>]*>)');
var searchresults = reg.exec(htmltext);

//Method 2:
//use the string.match() function
//var searchresults = htmltext.match('(<a class=l
[^>]*href\s*=\s*")([^"]*)("[^>]*>)');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top