1

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?

5 Answers 5

1
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
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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

Comments

0

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

2 Comments

The text might be like this : <a href="link1.php">1</a> There could be lots of text here<a href="link2.php">2</a> These links are between lots of simple text<a href="link3.php">3</a> So making this an XML may not be a very good idea?
As long as it is valid xml and all anchor tags are at the top level (not inside another p or div), this will work fine. If anchor tags are nested, you can use html..a instead of html.a inside for each to access all the descendants that are anchors.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.