JavaScript匹配函数返回null

时间:2017-10-11 18:46:26

标签: javascript android regex null match

我正在为Android游戏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
   }
}

1 个答案:

答案 0 :(得分:0)

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

但是

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

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