0

i have this link output from a facebook feed:

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

and i need the final ouput like this:

http://www.theguardian.com/travel/2014/apr/25/italy-puglia-salento-region

so basically i have this bit to remove:

 http://l.facebook.com/l.php?u=

i was trying a regex in javascript but not very familiar with it:

Body = document.getElementsByTagName("body")[0].innerHTML;
regex = ???
Matches = regex.exec(Body);

any ideas on how to make it work?

Libert Piou Piou
  • 540
  • 5
  • 22
manny999
  • 37
  • 7

3 Answers3

1

Use this :

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);

The function extractLinkFromFb() will return your link.

Libert Piou Piou
  • 540
  • 5
  • 22
0

Before decoding the url, you can use this regex to grab the piece you want:

var myregex = /u=([^&#\s]+)/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
    thematch = matchArray[1];
} else {
    thematch = "";
}
  • The parentheses in the regex captures the match to Group 1
  • u= serves as a delimiter, but is not captured in Group 1
  • [^&#\s] matches one character that is not a &, # or whitespace character. Tweak to suit.
  • the + quantifier matches one or more of these characters
zx81
  • 41,100
  • 9
  • 89
  • 105
0
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);
Majid
  • 35
  • 1
  • 7