2011-02-08 6 views
2

Json 응답을 반복하면서 앵커의 부분 (URL, 대상, 텍스트)을 추출하려고하는데 그렇게 할 수 없습니다. 나는 목표를 잡기 위해 위의 정규식을 수정하지 못하고앵커 태그에서 앵커 텍스트, URL 및 타겟을 추출하는 javascript 정규식

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 

:

나는이 길의 나에게 95 %를 가지고이 질문/답을 발견했다. 어떤 도움을 주시면 감사하겠습니다.

감사합니다. 난 당신이 jQuery를에 액세스 할 수 모르겠어요

답변

0

(또한이 기본 정규식보다는 아마 느립니다) 그러나 당신은 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를 활용할 수 있었고 이것이 트릭이었습니다. – Steve

+0

차가움. 나는 XML과 SVG 파일 같은 것들을 처리하는 유사한 트릭을 수행했다. 정규 표현식을 사용할 때마다 나는 그냥 쓸어 버린다. jQuery는 SOOO를 훨씬 쉽게 따라 할 수 있습니다. – peteorpeter

관련 문제