好吧,我有一个asp文件,它将推特上的rss提要拉到我的服务器上,我使用AJAX分解每个条目并编写HTML。我希望能够从条目的描述部分提取一个链接,但我在正确编写RegEx时遇到了问题。
$(entry).find('item').each(function() {
// gets the "id", "title", and "url" of current child element
$elm = $(this);
$title = $elm.find('title').text();
$desc = $elm.find('description').text();
$pubDate = $elm.find('pubDate').text();
$guid = $elm.find('guid').text();
$link = $elm.find('link').text();
$div.append('<div class="section" id="entry'+$count+'"><h3 class="pubDate">'+$pubDate.slice(0, -6)+'</h3><h3 class="desc">'+$desc+'</h3><div class="linkBox"><a href="'+$link+'" title="'+$title+'" class="link">'+$link+'</a></div></div>');
$href = $desc.match(/b(http|https)?(://)?(S*).(w{2,4})b/ig);
alert($href);
$count++
});
这就是我目前所拥有的:
这是示例tweet(原始字符串(:
I'm at Harrah's Hotel and Casino: Luxury Suite (New Orleans, LA) w/ 2 others http://t.co/UjxTIdiJ
我想提取链接使用这个:
$desc.match(/b(http|https)?(://)?(S*).(w{2,4})b/ig);
但它只返回:
http://t.co
我竭尽全力,试图让所有字符都超过http,直到成为空白字符,同时排除逗号等。
这个正则表达式应该能完成任务:s*(?i)hrefs*=s*("([^"]*")|'[^']*'|([^'">s]+))
。
示例:http://regex101.com/r/eL3wV4
或者,如果您没有内联,a href:(http:[^s]*)|(https[^s]*)
应该只为您提供http://*
或https://*
。
示例:http://regex101.com/r/uE5bZ5
好的,这是这个问题的答案,但https://stackoverflow.com/users/1472389/damien-overeem@Damian Overeem应该为向我展示regex101而获得所有赞誉,但以下是我如何选择我想要的:
$href = $desc.match(/b(http|https)?(://)?(S*).(w{2,4}(S*))b/ig);
在这里查看http://regex101.com/r/gT6hC2