[英]How do I get RegEx to get the entire URL… start at http and get everything after until a whitespace and how to exclude certain characters
好的,我有一個asp文件,將來自Twitter的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++
});
這是我到目前為止的內容:
這是示例推文(原始字符串):
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)href\\s*=\\s*(\\"([^"]*\\")|'[^']*'|([^'">\\s]+))
。
示例: http : //regex101.com/r/eL3wV4
或者,如果您沒有內聯,則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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.