1

使用正则表达式我想获取包含 .xls 或 .xlsx 文本的 href

我想坚持这个正则表达式

<a\s*[^>]*\s*href\s*=\s*((?:[^ ]|[\n\r])+)\s*[^>]*>.*?<\/a>

但是我应该在这里添加什么,以便我只能获取包含锚标记中的 .xls 或 .xlsx 文本的链接。

4

1 回答 1

0

这样做的许多潜在问题,但使用 JavaScript:

var re = new RegExp(/<a\s*[^>]*\s*href\s*=\s*((?:[^ ]|[\n\r])+)((\.xls)|(\.xlsx))\s*[^>]*>.*?<\/a>/ig);
txt = 'ok, here you go: <a href="test.xls">test file</a> and <a href="http://not.test.com">not file</a>, but another <a href = "http://www.xls.com/test.xls">test file</a>!';
txt.match(re)

=> ['<a href="test.xls">test file</a>', '<a href = "http://www.xls.com/test.xls">test file</a>']

于 2013-09-09T08:13:31.807 回答