使用javascript解析链接中的额外信息
我有一个facebook提要的链接输出:使用javascript解析链接中的额外信息,javascript,regex,facebook,Javascript,Regex,Facebook,我有一个facebook提要的链接输出: http://l.facebook.com/l.php?u=http%3A%2F%2Fwww.theguardian.com%2Ftravel%2F2014%2Fapr%2F25%2Fitaly-puglia-salento-region&h=2AQF4oNrg&s=1 我需要这样的最终输出: http://www.theguardian.com/travel/2014/apr/25/italy-puglia-salento-regio
http://l.facebook.com/l.php?u=http%3A%2F%2Fwww.theguardian.com%2Ftravel%2F2014%2Fapr%2F25%2Fitaly-puglia-salento-region&h=2AQF4oNrg&s=1
我需要这样的最终输出:
http://www.theguardian.com/travel/2014/apr/25/italy-puglia-salento-region
所以基本上我要去掉这一点:
http://l.facebook.com/l.php?u=
我试着用javascript编写正则表达式,但不是很熟悉:
Body = document.getElementsByTagName("body")[0].innerHTML;
regex = ???
Matches = regex.exec(Body);
关于如何让它工作,有什么想法吗?使用以下方法:
function extractLinkFromFb(fbLink) {
var encodedUri = fbLink.split('?u=');
return decodeURIComponent(encodedUri[1]);
}
var link = 'http://l.facebook.com/l.php?u=http%3A%2F%2Fwww.theguardian.com%2Ftravel%2F2014%2Fapr%2F25%2Fitaly-puglia-salento-region&h=2AQF4oNrg&s=1';
var myExtractedLink = extractLinkFromFb(link);
函数extractLinkFromFb()将返回您的链接。在解码url之前,您可以使用此正则表达式获取所需的片段:
var myregex = /u=([^&#\s]+)/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
thematch = matchArray[1];
} else {
thematch = "";
}
- 正则表达式中的括号捕获与组1的匹配
用作分隔符,但未在组1中捕获u=
匹配一个非[^&\s]
、&
或空白字符的字符。调整以适应#
量词与这些字符中的一个或多个匹配+
var url = 'http://l.facebook.com/l.php?u=http%3A%2F%2Fwww.theguardian.com%2Ftravel%2F2014%2Fapr%2F25%2Fitaly-puglia-salento-region&h=2AQF4oNrg&s=1';
var str1 = "http://l.facebook.com/l.php?u=";
var str2 = "&h=2AQF4oNrg&s=1";
var url = url.replace(str1, "");
var url=url.split("&h=")
var uri_dec = decodeURIComponent(url[0]); // =http://www.theguardian.com/travel/2014/apr/25/italy-puglia-salento-region
//alert (uri_dec);
//console.log (uri_dec);