2016-06-07 2 views
0

사용자가 이미지 링크가 포함 된 html 코드를 보내도록하는 양식을 작성 중입니다. 이처럼 : 특정 HTML 태그와 일치하는 정규식

<a href="http://linklocation.com" target="_blank"><img src="http://imagelocation.com" border="0"></a> 
<a href="http://linklocation.com" target="_blank"><img src="http://imagelocation.com" border="0"></a> 
<a href="http://linklocation.com" target="_blank"><img src="http://imagelocation.com" border="0"></a> 
<a href="http://linklocation.com" target="_blank"><img src="http://imagelocation.com" border="0"></a> 
<a href="http://linklocation.com" target="_blank"><img src="http://imagelocation.com" border="0" width="100px" height="100px"></a> <br> 

는 지금은 IMG가 <img />, <img> </img> 또는 <img> 할 수있는 유일한 aimg HTML 태그를 선택 정규식을 사용하려고 해요. 나는 지금 너비, 높이 또는 다른 어떤 것이 설정되어 있지는 않지만 RegEx와 함께해야합니다. 다른 HTML 태그가 있으면 RegEx와 함께 제공되어서는 안됩니다.

그래서 basicly 같은 HTML 코드가있는 경우 :

<a href="http://linklocation.com" target="_blank"><img src="http://imagelocation.com" border="0"></a> 
<a href="http://linklocation.com" target="_blank"><p>Hello world!</p></a> <script>Something</script> 
<a href="http://linklocation2.com" target="_blank"><img src="http://imagelocation2.com" border="0" width="200px" height="20px"></a> 

정규식은 다음을 반환해야합니다 :

<a href="http://linklocation.com" target="_blank"><img src="http://imagelocation.com" border="0"></a> 
<a href="http://linklocation2.com" target="_blank"><img src="http://imagelocation2.com" border="0" width="200px" height="20px"></a> 

난 당신이 내가 무엇을 찾고 이해를 바랍니다.

+1

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) 그래도이 작업을 수행하려면 맛 (당신은 어떤 언어입니까) –

답변

0

귀하의 정규식 솔루션입니다 :

/<a\s*.*?><img\s*.*?<\/a>/ 

PHP 예 :

$string = '<YOUR TEXT HERE>'; 
preg_match_all('#<a\s*.*?><img\s*.*?</a>#', $string, $matches); 
print_r($matches[0]); 

자바 스크립트 예 : [여기] 설명과 같이 (HTML을 구문 분석하는 정규식을 사용해서는 안

var string = '<YOUR TEXT HERE>'; 
var matches = string.match(/<a\s*.*?><img\s*.*?<\/a>/g); 
console.log(matches) 
관련 문제