【问题标题】:JavaScript match function returns nullJavaScript 匹配函数返回 null
【发布时间】:2017-10-11 18:46:26
【问题描述】:

我正在为 android 游戏 Word 小吃在 android 上创建一个程序。它将比较搜索到的字符串和文件中的单词(还没有从文件中,只是用字符串测试它)(参见规则)。但是,无论我做什么,我仍然收到错误:"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
   }
}

【问题讨论】:

    标签: javascript android regex null match


    【解决方案1】:

    没有

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

    但是

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

    您正在oring 正则表达式,而不是匹配结果。

    【讨论】:

      猜你喜欢
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多