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

时间:2013-12-11 05:58:05

标签: javascript

我正在尝试替换这样的字符串:

var thecurrentURL = 'IAmATextString=12345'
thecurrentURL = thecurrentURL.replace('IAmATextString', '').split('=');
thecurrentURLvalue = thecurrentURL[1];

如果我提醒这个

alert(thecurrentURLvalue);

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

if (thecurrentURLvalue == '12345' ) {
$('#Title').html('12345');
}

当我尝试检查它不起作用并返回

TypeError: thecurrentURL is undefined     
thecurrentURL = thecurrentURL.replace('IAmATextString', '').split('=');

有什么问题?谢谢!

1 个答案:

答案 0 :(得分:0)

检查以下代码:

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

请检查jsfiddle here