retrieving <a> tags in a string with regexp

F

Fred

Hi,

I would like to get an array of <a> html tags og a string.

I have done this but it doesnt works :

var reg;
var str='Bonjour, ceci est un test, une <a href="phrase.php">phrase</a>
décrivant un <img src="mot.gif" alt="mot"> assez <a
href="important.php">important</a>';

reg=new RegExp('<a [^<|>]+>([^<|>]*)<\/a>', "gi");

matchstr=reg.exec(str);
if (matchstr == null)
alert("No match");
else
{
alert(matchstr.length);
for (var i = 0; i < matchstr.length; i++)
{
alert(matchstr);
}

}


My BUG :
matchstr only contains <a href="phrase.php">phrase</a> bu not <a
href="important.php">important</a> !!


How to do then ?

Thanks for any help on it !

Regards

Fred
 
B

Bart Van der Donck

Fred said:
I would like to get an array of <a> html tags og a string.

I have done this but it doesnt works :

var reg;
var str='Bonjour, ceci est un test, une <a href="phrase.php">
phrase</a> décrivant un <img src="mot.gif" alt="mot"> assez <a
href="important.php">important</a>';

reg=new RegExp('<a [^<|>]+>([^<|>]*)<\/a>', "gi");

matchstr=reg.exec(str);
if (matchstr == null)
alert("No match");
else
{
alert(matchstr.length);
for (var i = 0; i < matchstr.length; i++)
{
alert(matchstr);
}

}


My BUG :
matchstr only contains <a href="phrase.php">phrase</a> bu not <a
href="important.php">important</a> !!


I would do something like

<pre>
<a href="http://www.google.com">1</a>
<A hRef="http://yahoo.com">2</A>
<a target="_blank" href = "file.php">3</a>
<a
href="javascript:alert('hi')" abc def>4</a>
<a href="#" OnClick="alert('hi')">5</a>
</pre>

<script type='text/javascript'>
for (var i=0; i<document.getElementsByTagName('a').length; i++)
alert(document.getElementsByTagName('a'));
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top