我只是想知道如何将我的 xmlhttp 请求转换成一个字符串,然后我可以对其执行操作?
假设我在变量“txt”中有这样的 xmlhttp 请求(我正在打开一个普通的 .html 文件进行阅读)
function handleXML(){
checkState(xmlhttp, function() {
var txt=xmlhttp.responseText + "";
txt.replace(/<[^>]*>/g, "");
//Convert txt into a string so that I can use it
});
}
编辑************************************************************************************************
为什么我想知道是因为下面的函数不能使用参数“txt”,但可以正常使用任何其他字符串参数
var buildName = "random";
var myvar = "";
myvar = lcs(txt, buildName);
function lcs(lcstest, lcstarget) {
this.lcstest = lcstest;//Don't work
this.lcstarget = lcstarget;
//But this works this.lcstest = "some random string"
matchfound = 0;
lsclen = lcstest.length;
for(lcsi=0; lcsi<lcstest.length; lcsi++){
lscos=0;
for(lcsj=0; lcsj<lcsi+1; lcsj++){
re = new RegExp("(?:.{" + lscos + "})(.{" + lsclen + "})", "i");
temp = re.test(lcstest);
re = new RegExp("(" + RegExp.$1 + ")", "i");
if(re.test(lcstarget)){
matchfound=1;
result = RegExp.$1;
break;
}
lscos = lscos + 1;
}
if(matchfound==1){return result; break;}
lsclen = lsclen - 1;
}
result = "fgh";
return result;
}
最佳答案
不需要做任何事情,只需将替换函数的返回值赋给变量本身就可以完成你的任务。
txt = txt.replace(/<[^>]*>/g, "");
编辑
您需要在 handleXML 函数之外声明您的 var txt
。在 lcs
函数中访问它的值。
var txt;
function handleXML(){
checkState(xmlhttp, function() {
txt=xmlhttp.responseText + "";
txt.replace(/<[^>]*>/g, "");
//Convert txt into a string so that I can use it
});
}
编辑
试试 this.lcstest = ""+ lcstest;
或者试试
this.lcstest = new string(lcstest)
关于javascript - 如何将 xmlhttprequest 转换为字符串 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6649603/