By default, the .
wildcard operator does not match newline characters (\n
, \r
). In other languages, there is a DOTALL
mode (sometimes called single line mode) to make .
match anything. Javascript doesn't have it for some reason. If you want the equivalent use [\s\S]
, which means any character is white space or is not white space so:
/<img alt="([\s\S]+?)" src="http:\/\/(.+?)\.(jpg|gif)">/
See Javascript regex multiline flag doesn’t work.
Also I escaped the .
before jpg|gif
otherwise it'll match any character and not the .
that you intend.
That being said, parsing HTML with regexes is a really bad idea. What's more, unless there is relevant detail missing from your question, you can do this easily with jQuery attribute selectors:
$("img[src='http://.*\.gif|jpg']").each(function() {
var alt = $(this).attr("alt");
var src = $(this).attr("src");
...
});
Or if you want there to be an alt
attribute:
$("img[alt][src='http://.*\.gif|jpg']").each(function() {
var alt = $(this).attr("alt");
var src = $(this).attr("src");
...
});