I'm attempting to replace url's in a string by calling a function that shortens the url's and then replaces the text. The shortening logic works perfectly, and I end up with the proper replacement url, however i cannot get the replace function to work correctly. Here is my code.
var newtext = shortenUrl(curText);
var shortenUrl = function (text) {
var exp = /(ftp|http|https):\/\/(?!bit\.ly)(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ig;
text.replace(exp,
function ($1) {
$.getJSON("http://api.bitly.com/v3/shorten?login=xxxxxx&apiKey=xxxx&longUrl=" + escape($1) + "&format=json",
function (result) {
return result.data.url;
});
});
return text;
};
result.data.url
contains a proper shortened url per the bit.ly API specs; however,
newtext
is always null for some reason.