[英]Regular Expression to extract HREFS
我正在尋找可以從中提取href的正則表達式:
<a href="/tr/blog.php?post=3593&user=930">
頁面上有數百個鏈接,因此我只需要提取包含以下內容的鏈接
/tr/blog.php
所以最后我應該留下以/ tr / blog開頭的鏈接列表
謝謝你的幫助。 這真的讓我感到困惑。
這是我當前正在使用的RegEx,但它與所有匹配。
/href\s*=\s*\"*[^\">]*/ig;
您可以嘗試使用href=\\"(/tr/blog.php[^"]*)\\"
(將捕獲到第1組),但是通常不應該使用regex來解析HTML 。
這有點晚了,但是現在已經是將來,您甚至不需要正則表達式:
document.querySelectorAll("a[href*='/tr/blog.php']")
將為您提供包含該字符串的鏈接,或者您可以找到以該字符串開頭的鏈接document.querySelectorAll("[href^='/tr/blog.php']")
。
<body> <a href="/tr/blog.php?lol">fslk</a>
<script>
var anchors = document.getElementsByTagName('a'), captured = [];
for ( var i = 0, l = anchors.length, href, r = /tr\/blog\.php/; i<l; ++i ) {
href = this.href;
if ( r.test( href ) ) {
captured.push( this )
}
}
// do what u want with captured links
for ( var l = captured.length; l--; ) {
alert( captured[l].href )
}
</script>
</body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.