By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

retrieving <a> tags in a string with regexp

P: n/a
Hi,

I would like to get an array of <ahtml 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[i]);
}

}
My BUG :
matchstr only contains <a href="phrase.php">phrase</abu not <a
href="important.php">important</a!!
How to do then ?

Thanks for any help on it !

Regards

Fred
Sep 14 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Fred wrote:
I would like to get an array of <ahtml 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</adé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[i]);
}

}
My BUG :
matchstr only contains <a href="phrase.php">phrase</abu 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')[i]);
</script>

--
Bart

Sep 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.