Skip to main content
Known Participant
October 6, 2014
Answered

ActionScript 3 String Match() Help

  • October 6, 2014
  • 1 reply
  • 800 views

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!

This topic has been closed for replies.
Correct answer z_abdulgalimov

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">

1 reply

z_abdulgalimov
Inspiring
October 6, 2014

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

rs14smithAuthor
Known Participant
October 6, 2014

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?

rs14smithAuthor
Known Participant
October 7, 2014

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">


Yeah that's what I actually started to do after I figured out that you can use the match() method.

Thanks!