Goal: Find all links to .doc(x), .xls(x), .ppt(x), zip, mov, mp3, mp4, vsd, and .pdf and add a lowercase (styled with smallcaps) filetype marker after each, unless the link contains an image.
$("a").filter( function(){
return (/.+\.(pdf|do[c|t]x?|xlsx?|pp[s|t]x?|zip|mp[3|4]|mov|vsd)/i).test($(this).attr('href'));
}).not(":has(img)").each( function(){
var h=$(this).attr('href').toLowerCase().replace(/.+\.(pdf|do[c|t]x?|xlsx?|pp[s|t]x?|zip|mp[3|4]|mov|vsd).*/, "$1");
$(this).after("<span class='marker'>"+h+"</span>");
});
You can change .filter() to .not() to reverse the logic
Why did I choose this option? Isn't using a regular expression slower than just natively building out a stack of natural selectors? -- yes, but the list of selectors can get ridiculously long, and I'd rather see this in my code:
$("a").filter(function(){
return (/.+\.(pdf|do[c|t]x?|xlsx?|pp[s|t]x?|pot|zip|mp[3|4]|mov|vsd)/i).test($(this).attr('href'));
})
$('a[href*=".pdf"],a[href*=".PDF"],a[href*=".Pdf"],
a[href*=".doc"],a[href*=".DOC"],a[href*=".Doc"],
a[href*=".docx"],a[href*=".DOCX"],a[href*=".Docx"],
a[href*=".dot"],a[href*=".DOT"],a[href*=".Dot"],
a[href*=".dotx"],a[href*=".DOTX"],a[href*=".Dotx"],
a[href*=".xls"],a[href*=".XLS"],a[href*=".Xls"],
a[href*=".xlsx"],a[href*=".XLSX"],a[href*=".Xlsx"],
a[href*=".pps"],a[href*=".PPS"],a[href*=".Pps"],
a[href*=".ppt"],a[href*=".PPT"],a[href*=".Ppt"],
a[href*=".pptx"],a[href*=".PPTX"],a[href*=".Pptx"],
a[href*=".pot"],a[href*=".POT"],a[href*=".Pot"],
a[href*=".zip"],a[href*=".ZIP"],a[href*=".Zip"],
a[href*=".mp3"],a[href*=".MP3"],a[href*=".Mp3"],
a[href*=".mp4"],a[href*=".MP4"],a[href*=".Mp4"],
a[href*=".mov"],a[href*=".MOV"],a[href*=".Mov"],
a[href*=".vsd"],a[href*=".VSD"],a[href*=".Vsd"]')
particularly if you need to combine numerous
:has()
and
:not()
options in that stack.