Yeti
Yeti

Reputation: 5818

How to extract text using regex in Actionscript?

In the HTML below:

<a href="link1.php">1</a><a href="link2.php">2</a><a href="link3.php">3</a>

How do I extract link1.php,link2.php,link3.php and push them into an array using regex? (There could be N number of <a> tags in the text)

[Edit] I'm aware the regex for this is something like href="([^"])*". But I'd like to know how to go about this in Actionscript. Specifically, how can they be pushed into an array in Actionscript?

Upvotes: 1

Views: 1455

Answers (5)

George Profenza
George Profenza

Reputation: 51847

Using RegExp.exec() will return an Object that you can access by index, like an array.

Also, you might find this and this post handy.

Upvotes: 0

jonathanasdf
jonathanasdf

Reputation: 2894

var str:String = '<a href="link1.php">1</a><a href="link2.php">2</a><a href="link3.php">3</a>';
var result:Array = str.split(/<a[^>]*href="([^"]*)".*?>.*?<\/a>/);
for (var i:int = 1; i < result.length; i += 2) {
    trace(result[i]);  // link1.php, link2.php, link3.php
}

Upvotes: 1

Amarghosh
Amarghosh

Reputation: 59451

Are you sure you want to use regex to parse html?

How about something like:

var anchors:String = '<a href="link1.php">1</a><a href="link2.php">2</a><a href="link3.php">3</a>';
var html = new XML('<p>' + anchors + '</p>');
var links:Array = [];
for each(var a:XML in html.a)
  links.push(String(a.@href));

Upvotes: 0

polygenelubricants
polygenelubricants

Reputation: 383756

The regex href="([^"])*" should work most of the time. It'd capture into \1. You may have to use this in a loop, and you may have to escape the ".

Upvotes: 0

Simon
Simon

Reputation: 9365

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

Upvotes: 0

Related Questions