如何从String中提取未知的URL

时间:2013-04-14 17:34:22

标签: javascript node.js express

我正在编写一个Node / Express应用程序,我在JSON对象中有一个文本字符串,我需要从中提取URL。 URL每次都不同,字符串本身有两个非常相似的URL,我只想拉出一个。

我唯一知道的是,在字符串中,网址将始终以相同的文字开头。

字符串:

The following new or updated things match your search criteria.

Link I Need
<http://randomurl.com/Junk/Yay/ThisView.aspx?r=164241242186&s=J
WD&t=JWD> 

Link I don't Need
<http://randomurl.com/Junk/Yay/ThisView.aspx?r=164241242186&s=J
WD&t=JWD&m=true> 

Search was last updated on April 12th, 2013 @ 14:43 

If you wish to unsubscribe from this update...

在此字符串中,我需要提取的是Link I Needhttp://randomurl.com/Junk/Yay/ThisView.aspx?r=164241242186&s=J WD&t=JWD下的网址,而不是其他内容。我不太清楚如何解决这个问题,非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效:

var s = "The following new or updated ...";
var regex = /Link I Need\s*<([^>]*)>/;
var match = s.match(regex);
var theUrl = match && match[1];

这假设URL不是跨换行分割的。如果是,那么在找到匹配后,您需要

theUrl = theUrl.replace(/\s+/, '')
相关问题