var re = /(?:[\s\S]*\.pdf|([\s\S]*))$/i; var lines = [ 'some string', 'some stringpdf', 'some string.pdf', 'some string.PDF' ]; for(var i = 0; i < lines.length; i++) { alert(re.exec(lines[i])[1]); }