JavaScript match 函数返回 null



我正在为安卓游戏Word零食创建一个程序。它将比较文件中搜索的字符串和单词(尚未从文件中搜索,只是使用字符串对其进行测试)(请参阅规则)。但是,无论我做什么,我仍然得到错误:"Script Error: Cannot read property 'length' of null"在线:

if((from_file.match(new RegExp(from_file[i], "g") || [])).length != (searched.match(new RegExp(from_file[i], "g") || [])).length)

在功能compare_words(from_file, searched) .我正在使用(string.match(new RegExp(searched_str, "g") || [])).length来查找字符串中的出现次数,如堆栈溢出上已经显示的那样。但我仍然收到这个错误。有人可以帮助我吗?比你。:D

function OnStart()
{
    lay = app.CreateLayout( "linear", "" );
    var from_file = "dedo";
    var searched = "odod";
    if(compare_words(from_file, searched) == true)
    {
        txt = app.CreateText(from_file + ": true");
        txt.SetTextSize( 32 );
        lay.AddChild( txt );
    }
    app.AddLayout( lay );
}

function compare_words(from_file, searched)
{
   if(from_file.length == searched.length)
   {
       var b = 0;
       for(i = 0; i < from_file.length; i++)
       {
           if((from_file.match(new RegExp(from_file[i], "g") || [])).length != (searched.match(new RegExp(from_file[i], "g") || [])).length)
           {
               b = 1;
               break;
           }
       }
       if(b == 0)
       {
           return true
       }
       else
       {
           return false
       }
   }
   else
   {
       return false
   }
}

not

(from_file.match(new RegExp(from_file[i], "g") || [])).length

(from_file.match(new RegExp(from_file[i], "g")) || []).length

or的是正则表达式,而不是匹配结果。

相关内容

  • 没有找到相关文章

最新更新