Uncaught: TypeError: Cannot read property 'toString' of null
Uncaught: TypeError: Cannot read property 'toString' of null
这里是菜鸟,我目前正在 javascript 学习正则表达式。我有一个函数,应该从字符串中选择 0 到 9 之间的数字。只要它搜索的变量包含字母或数字,该函数就可以正常工作,但是当输入空值时,它会给出以下错误:
未捕获:TypeError:无法读取 属性 'toString' of null
我该如何解决这个问题?提前谢谢你。
代码如下:
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!");
};
有几种方法可以检查这一点。而且应该不难在网上找到任何东西:)
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
如果你做一些 console.log
ging 你会看到 lause.match()
returns 找到匹配项的数字数组。
你的情况:
["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()
String.prototype.match() returns null
如果没有找到匹配项。
因此,您有两种选择来处理此问题
- 检查
setti
是否有真值,你可以这样 if(setti)
.
- 像
setti?.toString()
一样使用Optional chaining(?.)
,.
关于您的代码的几点注意事项:
方法test()接受一个字符串参数,这使得这不正确var testi = numerot.test(0-9);
代码不在函数内部function tulostanumerot();
你完全可以省略testi
,只用setti
请注意,match() returns 要么是数组,要么是 null,因此您可以使用 if (setti) {
而不是检查 true
代码可能看起来像
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));
这里是菜鸟,我目前正在 javascript 学习正则表达式。我有一个函数,应该从字符串中选择 0 到 9 之间的数字。只要它搜索的变量包含字母或数字,该函数就可以正常工作,但是当输入空值时,它会给出以下错误:
未捕获:TypeError:无法读取 属性 'toString' of null
我该如何解决这个问题?提前谢谢你。
代码如下:
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!");
};
有几种方法可以检查这一点。而且应该不难在网上找到任何东西:)
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
如果你做一些 console.log
ging 你会看到 lause.match()
returns 找到匹配项的数字数组。
你的情况:
["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()
String.prototype.match() returns null
如果没有找到匹配项。
因此,您有两种选择来处理此问题
- 检查
setti
是否有真值,你可以这样if(setti)
. - 像
setti?.toString()
一样使用Optional chaining(?.)
,.
关于您的代码的几点注意事项:
方法test()接受一个字符串参数,这使得这不正确
var testi = numerot.test(0-9);
代码不在函数内部
function tulostanumerot();
你完全可以省略
testi
,只用setti
请注意,match() returns 要么是数组,要么是 null,因此您可以使用
if (setti) {
而不是检查 true
代码可能看起来像
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));