Click to See Complete Forum and Search --> : regexp for extracting IMG tags


dbalatero
05-17-2003, 11:34 AM
I need to extract the src="" part of all IMG tags from a variable filled with HTML code, eg:

<script>
var myHtml = "blah blah<p>blah<img src=\".....\">blah<a>...";

var myRegexp = /regexp/flags;
var matches = myHtml.match(myRegexp);
</script>

If anyone could help me with this (I'm HORRIBLE with regular expressions), that would be so great.

Thanks,
David Balatero

Jona
05-17-2003, 11:48 AM
You can say document.images["imageName"].src instead of doing it manually, unless you're parsing a string.

Nevermore
05-17-2003, 02:09 PM
You could take a substirng based on indexOf('<img src="');

Charles
05-17-2003, 03:23 PM
Originally posted by cijori
You could take a substirng based on indexOf('<img src="'); No, you can't.

1) HTML is case insensitive.

2) In HTML the quotes are often optional and omitted.

3) HTML recognizes both single and double quotes.

4) There are likely to be any number of other attributes between 'img' and 'src'.

If we were conserned here with XHTML then we would only have to concern ourselves with numbers 3 and 4 and Regular expressions would work. But this is supposed to work with HTML so a full blown SGML parser will be necessary.

dbalatero
05-17-2003, 10:05 PM
It is XHTML actually, sorry for not adding that. Which would definitely change things...

Nevermore
05-18-2003, 03:40 AM
I live in a sheltered world, where people write in lower case, and nothing goes between the tag and its source.