Highlighted

ActionScript 3 String Match() Help

New Here ,
Oct 05, 2014

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="/somename/sameForAllFiles/file.sz">,<a href="/somename/sameForAllFiles/image.png">,<a href="/somename/sameForAllFiles/video.flv">

TOPICS
Development

Views

401

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

ActionScript 3 String Match() Help

New Here ,
Oct 05, 2014

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="/somename/sameForAllFiles/file.sz">,<a href="/somename/sameForAllFiles/image.png">,<a href="/somename/sameForAllFiles/video.flv">

TOPICS
Development

Views

402

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 05, 2014 0
Explorer ,
Oct 06, 2014

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2014 0
New Here ,
Oct 06, 2014

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2014 0
Explorer ,
Oct 06, 2014

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);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2014 0
Explorer ,
Oct 06, 2014

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2014 0
New Here ,
Oct 07, 2014

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 07, 2014 0