Javascript普通数组值与match函数创建的数组值比较

Javascript normal Array value and Array value created by match function comparison

本文关键字:数组 创建 比较 函数 Javascript match      更新时间:2023-09-26

我有两种类型的数组值比较的问题,这里是

  tagNames = [];
            tagNames.push('61');
            cmt_wrds = '‏‏61'.replace(/[`~!@#$%^&*()_|+'-=?;:'",،؛«».<>'{'}'[']'''/]/gi, ' ').match(/'S+/g);

            if ( tagNames[0] == cmt_wrds[0] ) { // issue is here
                console.log('yes'); // --> nothing
            };

如果您记录您的变量,您将看到它们有点不同。它把

''u200f' 

符号,即从右到左标记

var tagNames = [];
 tagNames.push('61');
 cmt_wrds = '‏‏61'.replace(/[`~!@#$%^&*()_|+'-=?;'u200f:'",،؛«».<>'{'}'[']'''/]/gi, ' ').match(/'S+/g);
console.log(tagNames);
console.log(cmt_wrds);
console.log(tagNames[0] === cmt_wrds[0]); // returns false, because they are different

使用一些变量来给出一个输入数据,它将解决您的问题。

(function () {
   tagNames = [];
                tagNames.push('61');
  var datas ="61";//variable declaration
                cmt_wrds = datas.replace(/[`~!@#$%^&*()_|+'-=?;:'",،؛«».<>'{'}'[']'''/]/gi, ' ').match(/'S+/g);
 
                if ( tagNames[0] == cmt_wrds[0]) { // issue is here
                    console.log('yes'); // --> nothing
                };
  
  })()

如果您显示它们的长度,则差异如下。

tagNames[0].length = 2

cmt_wrds[0].length = 4

tagNames = [];
            tagNames.push('61');
            cmt_wrds = '‏‏61'.replace(/[`~!@#$%^&*()_|+'-=?;:'",،؛«».<>'{'}'[']'''/]/gi, ' ').match(/'S+/g);
            if ( tagNames[0] == cmt_wrds[0] ) { // issue is here
                console.log('yes'); // --> nothing
            };
console.log(tagNames[0].length);
console.log(cmt_wrds[0].length);