Check if link (not URL) contain some text (.jpg, .gif, .png)

javascript, jquery

Solution

Here's my first instinct:

$('.maybe .link').each(function () {
    if ($(this).text().toLowerCase().match(/\.(jpg|png|gif)/g)) {
        console.log("yay I did it");
    }
});

Use toLowerCase() on the link text so you don't have to check both lower and upper case. Then use String.match(regex) with a regex group to match all the file extensions.

Hope this helps!

Edit: here's an example in jsfiddle. Open your javascript console to see the output of the console.log statement. http://jsfiddle.net/9Q5yu/1/

Problem

Just to give an Idea what i'm trying to do here's an example code: ``` $(function(){ if ($('.maybe > div > a.link:contains(".JPG, .jpg, .gif, .GIF")').length) { alert('hello'); }); ``` I want to check if the content of some links are containing the dot and the letters of all image extensions, like ``` <div class="maybe"> <div> <a class="link" href="someURL">thisIsAnImage.jpg</a> <a class="link" href="someURL">thisIs**NOT**AnImage.pdf</a> </div> </div> <div class="maybe"> <div> <a class="link" href="someURL">thisIs**NOT**AnImage.zip</a> <a class="link" href="someURL">thisIsAnotherImage.png</a> </div> </div> ``` The div's and links are generated dynamically by php, so there's no way to know how many links and div's there will be once the page is generated. How to write the code in a properply way? Thanks a lot for helping me to resolve the problem.

Original source

Related problems