【问题标题】:Uncaught: TypeError: Cannot read property 'toString' of null未捕获:TypeError:无法读取 null 的属性“toString”
【发布时间】:2021-08-01 11:44:52
【问题描述】:

这里是菜鸟,我目前正在学习 javascript 中的正则表达式。我有一个函数应该从字符串中选择 0 到 9 之间的数字。只要它搜索的变量有字母或数字,该函数就可以正常工作,但是当输入空值时,它会给出以下错误:

未捕获:TypeError:无法读取 null 的属性“toString”

我该如何解决这个问题?提前谢谢你。

代码如下:

var lause  = "s0m3 p30pl3";
function tulostanumerot();
var numerot = /[0-9]/g;
   var testi = numerot.test(0-9);

   var setti = lause.match(numerot);
   if (testi === true) {
    console.log(setti.toString());
}
   else {
   console.log("Ei numeroita!");
};

【问题讨论】:

  • 这部分看起来不正确var testi = numerot.test(0-9);
  • 函数声明不正确;没有函数体。
  • 您能否提供一个显示错误的minimal reproducible example,您提供的代码不会产生您在问题中描述的类型错误

标签: javascript regex null


【解决方案1】:

关于您的代码的几点说明:

  • test() 方法接受一个字符串参数,这使得 var testi = numerot.test(0-9); 不正确

  • 代码不在函数function tulostanumerot();

  • 你可以完全省略testi,只使用setti

  • 请注意,match() 返回一个数组或 null,因此您可以使用 if (setti) { 而不是检查是否为真

代码可能看起来像

function tulostanumerot() {
    var numerot = /[0-9]/g;
    var setti = lause.match(numerot);

    if (setti) {
        console.log(setti.toString());
    } else {
        console.log("Ei numeroita!");
    }
}
tulostanumerot();

function tulostanumerot(lause) {
  var numerot = /[0-9]/g;
  var setti = lause.match(numerot);

  if (setti) {
    console.log(setti.toString());
  } else {
    console.log("Ei numeroita!");
  }
}

[
  "s0m3 p30pl3",
  "",
  "test"
].forEach(v => tulostanumerot(v));

【讨论】:

    【解决方案2】:

    如果没有找到匹配项,String.prototype.match() 将返回 null

    所以,你有两个选择来处理这个

    1. 检查setti是否有真值,你可以这样if(setti)
    2. 使用Optional chaining (?.) 就像setti?.toString(),.

    【讨论】:

      【解决方案3】:

      如果您执行一些console.logging,您将看到lause.match() 返回找到匹配项的数字数组。

      在你的情况下:

      ["0", "3", "3", "0", "3"]
      

      您收到错误消息,因为如果找不到匹配项,setti 将为空。我们可以这样检查。

      if (setti) {
         // Setti is not undefined
      }
      

      如果你想将元素组合成一个字符串,你可以使用.join

      if (setti) {
        console.log(setti.join());
      } else {
        console.log("Ei numeroita!");
      };
      

      完整代码:

      var lause = "s0m3 p30pl3";
      
      function tulostanumerot() {
        var numerot = /[0-9]/g;
        var setti = lause.match(numerot);
        
        if (setti) {
          console.log(setti.join(""));
        } else {
          console.log("Ei numeroita!");
        };
      }
      
      var lause = "s0m3 p30pl3";
      tulostanumerot()
      
      var lause = "no numbers here";
      tulostanumerot()

      【讨论】:

      • OPs 代码崩溃,因为 settinull(无论出于何种原因),因此当您尝试在 null 上使用 .join() 时,您会遇到类似的问题
      • @NickParsons 没有可选链接。但我想我明白 OP 现在想要什么了。
      • 哦,我没有注意到可选的链接编辑,那看起来不错?
      【解决方案4】:

      有几种方法可以检查这一点。并且在网上找到任何东西都不难:)

      if(lause) //Will check if there is any value to the string, which is a non false value. Example: 0, null, undefined false will all fail this statement
      
      OR
      
      if(typeof lause !== "undefined") //Simple but will also pass Arrays and objects
      
      if(typeof lause  === "string" && typeof lause  === "number") //Will only be passed if matching. So if the variable is a array or object it will not be passed
      

      【讨论】:

        猜你喜欢
        • 2022-11-20
        • 2022-11-24
        • 2022-12-20
        • 1970-01-01
        • 1970-01-01
        • 2017-07-15
        • 2021-04-02
        • 2021-11-22
        • 1970-01-01
        相关资源
        最近更新 更多