所以,这是我的功能:它应该从输入中提取链接。 (从http或类似的东西开始,以“”结束)。问题是我无法调试这个函数的工作情况,因为它停留在循环中。我试图检查原因,仍然无法分辨。
function linkify(input)
{
if (input === undefined || input == null)
return input;
var tinput = input;
var urlextr = "";
var url = [""];
var num = 0;
//search for "http://" first
while(tinput.match(/http:\/\//gi) != null)
{
console.log("http");
urlextr=tinput.substring(tinput.search("http://"));
urlextr=urlextr.substring(0,urlextr.search(" "));
url[num] = urlextr;
url[num+1] = urlextr;
num = num + 2;
tinput = tinput.replace(urlextr,"");
}
//search for "https://"
while(tinput.match(/https:\/\//gi) != null)
{
console.log("https");
urlextr=tinput.substring(tinput.search("https://"));
urlextr=urlextr.substring(0,urlextr.search(" "));
url[num] = urlextr;
url[num+1] = urlextr;
num = num + 2;
tinput = tinput.replace(urlextr,"");
}
//search for "ftp://" (Why not?)
while(tinput.match(/ftp:\/\//gi) != null)
{
console.log("ftp");
urlextr=tinput.substring(tinput.search("ftp://"));
urlextr=urlextr.substring(0,urlextr.search(" "));
url[num] = urlextr;
url[num+1] = urlextr;
num = num + 2;
tinput = tinput.replace(urlextr,"");
}
//search for "www.*" (Must be last!)
while(tinput.match(/www./gi) != null)
{
console.log("www");
urlextr=tinput.substring(tinput.search("www."));
urlextr=urlextr.substring(0,urlextr.search(" "));
url[num] = urlextr;
url[num+1] = urlextr;
num = num + 2;
tinput = tinput.replace(urlextr,"");
}
console.log(url);
return output;
}
答案 0 :(得分:2)
我假设您有一个字符串,其中您有不同的空格分隔的网址。问题在于
urlextr=urlextr.substring(0,urlextr.search(" "));
如果您正在处理字符串中的最后一个URL
,它将始终返回一个空字符串,从而使循环无限期地运行。要解决它,你可以像下面那样做
urlextr=tinput.substring(tinput.search("http://"));
urlextr=urlextr.substring(0,urlextr.search(" "));
if (!urlextr) {
url[num] = tinput.trim();
break;
}
url[num] = urlextr;
url[num+1] = urlextr;