Copy link to clipboard
Copied
Hi,
I have a several hyperlinks on a webpage, and I only need to store a few that have a certain pattern in a variable/array.
I've discovered you can do this with the match() function. I've loaded the contain on the webpage into a string, so I'm simply using the match function to try to find the pattern. The problem is, I'm not that great with reg expressions.
The hyperlinks that I want to catch are written like the following: <a href="/somename/sameForAllFiles/file.sz">Some Link</a>
I only need to store the content between the href=" and ">. The "sameForAllFiles" folder is what is common for all the files and what I'm trying to use in the match function, but I just can't get the expression right. Can someone write some basic sample code that I could use?
Thanks!
If using the method "match", so that:
var string:String = 'blablabla\
<a href="/somename/sameForAllFiles/file.sz">Some Link</a> blablabla\
<a href="/somename/sameForAllFiles/image.png">Some Link2</a>\
<a href="/somename/sameForAllFiles/video.flv">Some Link3</a>';
//
var directoryTemplate:String = '/sameForAllFiles/';
//
var regSource:String = '<a[^<>]+href="[^"]+'+directoryTemplate+'[^"]+"[^<>]*>';
var reg:RegExp = new RegExp(regSource, 'g');
//
trace(string.match(reg));
Result:
<a href="/somenam
...Copy link to clipboard
Copied
in this way?
var string:String = 'blablabla <a href="/somename/sameForAllFiles/file.sz">Some Link</a> blablabla';
var directoryTemplate:String = '/sameForAllFiles/';
//
var regSource:String = '<a[^<>]+href="(([^"]+)'+directoryTemplate+'([^"]+))"[^<>]*>';
var reg:RegExp = new RegExp(regSource, 'g');
var exec:Object = reg.exec(string);
//
// full url
trace(exec[0]); // <a href="/somename/sameForAllFiles/file.sz">
// full href
trace(exec[1]); // /somename/sameForAllFiles/file.sz
// href base path
trace(exec[2]); // /somename
// href filename
trace(exec[3]); // file.sz
Copy link to clipboard
Copied
This works, however, what if I have "multiple" links on that page with the same pattern. How do I access the other links as well in the exec array?
Copy link to clipboard
Copied
You can do this:
var string:String = 'blablabla\
<a href="/somename/sameForAllFiles/file.sz">Some Link</a> blablabla\
<a href="/somename/sameForAllFiles/image.png">Some Link2</a>\
<a href="/somename/sameForAllFiles/video.flv">Some Link3</a>';
//
var directoryTemplate:String = '/sameForAllFiles/';
//
var regSource:String = '<a[^<>]+href="(([^"]+)'+directoryTemplate+'([^"]+))"[^<>]*>';
var reg:RegExp = new RegExp(regSource, 'g');
//
// start from index = 0
reg.lastIndex = 0;
//
var exec:Object;
do {
trace();
// current index
trace('exec from index:'+reg.lastIndex);
exec = reg.exec(string);
if (exec) {
// result
trace(exec[0]);
trace(exec[1]);
trace(exec[2]);
trace(exec[3]);
} else {
// end
}
} while(exec);
Copy link to clipboard
Copied
If using the method "match", so that:
var string:String = 'blablabla\
<a href="/somename/sameForAllFiles/file.sz">Some Link</a> blablabla\
<a href="/somename/sameForAllFiles/image.png">Some Link2</a>\
<a href="/somename/sameForAllFiles/video.flv">Some Link3</a>';
//
var directoryTemplate:String = '/sameForAllFiles/';
//
var regSource:String = '<a[^<>]+href="[^"]+'+directoryTemplate+'[^"]+"[^<>]*>';
var reg:RegExp = new RegExp(regSource, 'g');
//
trace(string.match(reg));
Result:
<a href="/somename/sameForAllFiles/file.sz">,<a href="/somename/sameForAllFiles/image.png">,<a href="/somename/sameForAllFiles/video.flv">
Copy link to clipboard
Copied
Yeah that's what I actually started to do after I figured out that you can use the match() method.
Thanks!