js 正则 匹配html a标签 .pdf文件

clipboard.png
文章是后台上传的,前端需要再html中通过正则找到.pdf后缀,进行对标签的一些js操作。希望大神帮忙写个正则帮忙找出所有.pdf 文件

阅读 4.4k
2 个回答

没看明白你到底是要找到有 pdf 文件的 a 元素, 还是要 pdf 完整地址? 不管是哪个, 都不建议用正则.

const links = document.querySelectorAll('a[href$=".pdf"]'); // 所有链接地址以 `.pdf` 结尾的 a 元素
Array.prototype.slice.call(links).map(link => link.getAttribute('href')); // 所有 pdf 地址
$("a:contains(.pdf)")
//这样更好些
推荐问题