• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

ActionScript 3 String Match() Help

New Here ,
Oct 05, 2014 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!

TOPICS
Development

Views

543

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
community guidelines

correct answers 1 Correct answer

Explorer , Oct 06, 2014 Oct 06, 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="/somenam

...

Votes

Translate

Translate
Explorer ,
Oct 06, 2014 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

Votes

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
community guidelines
New Here ,
Oct 06, 2014 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?

Votes

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
community guidelines
Explorer ,
Oct 06, 2014 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);

Votes

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
community guidelines
Explorer ,
Oct 06, 2014 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">

Votes

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
community guidelines
New Here ,
Oct 07, 2014 Oct 07, 2014

Copy link to clipboard

Copied

LATEST

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

Thanks!

Votes

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
community guidelines