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

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


请检查jsfiddle here

10-06 00:03