I'm trying to match all anchors on a page that link to files with certain extensions, but the code is also catching cases where the URL ends with the extension text, but doesn't contain the actual extension (no period).
For example, it should (and does) match http://example.com/image.jpg, but it's also matching http://example.com/imagejpg (which I don't want it to).
The code used is:
var imageExtensions = "jpg|jpeg|gif|png|svg";
var allAnchors = document.getElementsByTagName("a");
for(var i = 0; i < allAnchors.length; i++) {
var anchorWithImage = allAnchors[i];
var matcher = new RegExp(".*\.(" + imageExtensions + ")$");
if(matcher.test(anchorWithImage.href)) {
alert(anchorWithImage.href);
}
}
I'm under the impression that this should require that the extension text is at the end of the string, a literal period is before the extension, and there can be anything before the extension. I don't see why the literal period is being ignored.
For real test data, I was running this script against http://www.reddit.com/r/gifs/comments/2bis0x/holy_shit_greg/ and it matches http://www.reddit.com/r/makemeagif, which doesn't have a literal period. Running these links against this Regex tester has the expected results.