开发者

can't get JS to correctly evaluate two identical strings

开发者 https://www.devze.com 2023-02-01 06:37 出处:网络
I can see in the log开发者_JAVA百科 that there are multiple matches during the loop but it\'s only returning true (and adding the file to the array) on the last iteration. \'typeof\' is telling me the

I can see in the log开发者_JAVA百科 that there are multiple matches during the loop but it's only returning true (and adding the file to the array) on the last iteration. 'typeof' is telling me they are all strings and they look identical in the console. can't figure what would make JS not see them as identical.

Would spaces or square brackets cause problems?

var name = file.split('.');
for (l=0;l<d.length;l++)
{
  var n = d[l].split('.');
  var r = n[0].replace(/\[\d\]$/,''); //chop off last bracket in file name

  // tells me both variables are strings
  console.log('r is a: ' + (typeof r) + ' name is a: ' + (typeof name[0]));

  if(r && r == name[0])
  {
    count_array.push(r);
    console.log('>>>>>>>>>>>>>>>' + r + ' added to array');
  }
}

Thx!


Your question is, er, far from clear. But a stab in the dark, you have this line:

var r = n[0].replace(/\[\d\]$/,''); //chop off last bracket in file name

That will remove a paired set of brackets with one digit in-between them, so for instance foo[1] => foo. It won't remove a paired set of brackets with two or more digits in-between them, like say foo[10] => foo. For that, you'd want:

var r = n[0].replace(/\[\d+\]$/,''); //chop off last bracket in file name
//                        ^--- Added the +
0

精彩评论

暂无评论...
验证码 换一张
取 消