Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript字符串替换错误:变量未定义_Javascript - Fatal编程技术网

Javascript字符串替换错误:变量未定义

Javascript字符串替换错误:变量未定义,javascript,Javascript,我正在尝试替换如下字符串: var thecurrentURL = 'IAmATextString=12345' thecurrentURL = thecurrentURL.replace('IAmATextString', '').split('='); thecurrentURLvalue = thecurrentURL[1]; 如果我像这样提醒你 alert(thecurrentURLvalue); 它返回正确的字符串。现在我想检查字符串是否正确,并将其写入div if (thecur

我正在尝试替换如下字符串:

var thecurrentURL = 'IAmATextString=12345'
thecurrentURL = thecurrentURL.replace('IAmATextString', '').split('=');
thecurrentURLvalue = thecurrentURL[1];
如果我像这样提醒你

alert(thecurrentURLvalue);
它返回正确的字符串。现在我想检查字符串是否正确,并将其写入div

if (thecurrentURLvalue == '12345' ) {
$('#Title').html('12345');
}
当我试图检查它不工作,并返回

TypeError: thecurrentURL is undefined     
thecurrentURL = thecurrentURL.replace('IAmATextString', '').split('=');
有什么问题?谢谢大家!

检查以下代码:

var thecurrentURL = 'IAmATextString=12345'
thecurrentURL = thecurrentURL.split('=');//Array Contains ['IAmATextString','12345']
//thecurrentURL.replace('IAmATextString', '').split('=');//Array Contains ['12345'] Index 0 only available here 
thecurrentURLvalue = thecurrentURL[1];

请检查JSFIDLE

是否缺少“;”在var的末尾,currentURL='IAmATextString=12345'@hicurin不会有什么不同,这里一定有其他问题,Marc,我没有看到任何问题。这看起来很好-你确定你的代码中没有额外的作用域吗?你发布的代码没有任何问题。没有更多的信息,我们无法帮助你。嗯,很奇怪。稍后我将提供更多代码。请给我一点时间用小提琴重现这个问题,你是说它应该是“当前URL[0];”?奇怪的是,当我提醒它时,它返回“12345”。但如果我用if条件检查它,它就不起作用了