【问题标题】:How to get href text which contains .xls or .xlsx text in an anchor tag using regex如何使用正则表达式在锚标记中获取包含 .xls 或 .xlsx 文本的 href 文本
【发布时间】:2013-09-09 07:41:44
【问题描述】:

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

我想坚持这个正则表达式

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

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

【问题讨论】:

标签: c# regex


【解决方案1】:

这样做有很多潜在的问题,但使用 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)

=&gt; ['&lt;a href="test.xls"&gt;test file&lt;/a&gt;', '&lt;a href = "http://www.xls.com/test.xls"&gt;test file&lt;/a&gt;']

【讨论】:

  • 任何其他建议
  • Corak 的建议会起作用,或者使用 JavaScript 只需使用 document.getElementsByTag ('a') 遍历 DOM 并在每个 src 属性上使用更简单的正则表达式。
猜你喜欢
  • 1970-01-01
  • 2017-02-13
  • 2021-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多