2016-04-26 79 views
1

我有以下的html代码。我想获得第二个“https://regex101.com/#javascript”的href值。我尝试使用表达式:<a class="tt-website" href=\"(.*)\">(.*)<\/a>然后我使用了JavaScript exec()方法。从返回的阵列我得到https://regex101.com/#javascript" target="new使用正则表达式获取特定的href值

<div class="row social-media-row"> 
<ul class="tt-wrapper"> 
    <li><a class="tt-map popup-gmaps" href="https://www.google.com/maps"><span>Map</span></a></li> 
    <li><a class="tt-website" href="https://regex101.com/#javascript" target="new"><span>Website</span></a></li> 
    <li><a class="tt-facebook" href="https://www.facebook.com/" target="new"><span>Facebook</span></a></li> 
    <li><a class="tt-video popup-gmaps" href=""><span>Video</span></a></li> 
</ul> 
</div> 

任何帮助表示赞赏。

+0

任何理由不被它的类名和访问它通过获得元素传统的API?你可能根本不需要正则表达式。 – Yoda

+0

尤达我不能这样做,因为代码实际上是来自textarea输入的文本 –

回答

1

除了使用贪婪.*的,使用此:

<a class="tt-website" href="([^"]+)" 
+0

非常感谢! –

+0

@Panagiotis:不客气,很高兴帮助 – Toto

0

var re = /href="(.*)"\s+/g; 
 
var str = '<a class="tt-website" href="https://regex101.com/#javascript" target="new"><span>Website</span></a>'; 
 
var m; 
 
    
 
m = str.match(re); 
 

 
document.write(m);

https://regex101.com/r/yY3gR0/1