2011-02-08 101 views
2

我試圖在我通過Json響應循環時提取錨點的部分(URL,目標,文本)並且無法這樣做。javascript正則表達式從錨定標記中提取錨文本,URL和目標

我發現這個問題/答案,讓我95%的方式出現:

javascript regex to extract anchor text and URL from anchor tags

var input_content = "blah \ 
    <a href=\"http://yahoo.com\">Yahoo</a> \ 
    blah \ 
    <a href=\"http://google.com\">Google</a> \ 
    blah"; 

var matches = []; 

input_content.replace(/[^<]*(<a href="([^"]+)">([^<]+)<\/a>)/g, function() { 
    matches.push(Array.prototype.slice.call(arguments, 1, 4)); 
}); 

alert(matches.join("\n")); 

//Gives 

//<a href="http://yahoo.com">Yahoo</a>,http://yahoo.com,Yahoo 
//<a href="http://google.com">Google</a>,http://google.com,Google 

我一直沒能修改上述正則表達式來獲取目標。任何幫助,將不勝感激。

謝謝。

回答

0

我不知道你有機會獲得的jQuery(這也可能是比原來的正則表達式慢),但你可以從JSON響應中提取標記字符串,敷在jQuery的,便於人類可讀的處理:

$links.find('a').each(function(){ 
    var text = $(this).text(); 
    var target = $(this).attr('target'); 
    var href = $(this).attr('href'); 

    // Do whatever you were going to do 
}); 
+0

我能夠利用jQuery,並做了這個訣竅,thx。 – Steve 2011-02-09 15:00:17

相關問題