On a recent WordPress project I had a requirement that any files be opened in a new tab or window. Now this can be easily accomplished by the users when they create the post or page by linking to the file and marking the checkbox “Open Link In A New Window/Tab”. But we all know users can’t be trusted so in order to “double check” them I wrote a WordPress plugin that will hunt the page for anchor tags that link to something with a file extension, and simply add “target=’_blank'” to them.
Basically all the plugin does is enqueue a jQuery script that does the work. I got the idea to use the jQuery .filter() method from @nickf and the regular expression (because I suck at them) from @már-Örlygsson.