[英]How to get href text which contains .xls or .xlsx text in an anchor tag using regex
使用正則表達式,我想獲取包含.xls或.xlsx文本的href
我想堅持這個正則表達式
<a\s*[^>]*\s*href\s*=\s*((?:[^ ]|[\n\r])+)\s*[^>]*>.*?<\/a>
但是我應該在這里添加些什么,以便我只能獲得錨定標記中包含.xls或.xlsx文本的鏈接。
這樣做會導致很多潛在的問題,但是要使用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>']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.